• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why the min of java byte type is -128 and not -127 or -255

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

I know that the type byte has as size of 8 bits  and the first bit  is used for sign this type has a range  from -128 to +127 ok.
I undrstand that 0111 1111 = 127 .
But for negative numbers when the first byte is 1 like below:
1111 1111 why itis not -255 or -127.
I want to know for  1000 0001  is it -129 or -1 or -127.

Thanks for you all.
 
Saloon Keeper
Posts: 7601
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because integers are stored using https://en.wikipedia.org/wiki/Two%27s_complement
 
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a simple reason for this: ease of hardware implementation.

If you take the bit sequence 1111 1111 and add 1 to it, you get the bit sequence 1 0000 0000. The leftmost bit is then discarded.

So if you add 1 to 1111 1111 and it naturally results in the integer 0, then what integer should the bit sequence 1111 1111 represent? That's right, the integer -1.
 
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moussa Kheyar wrote:. . . the first bit  is used for sign . . .

That is not quite accurate; Java┬«'s integer primitive types are not defined in sign and magnitude (=S&M) format, but unsigned (chars) or two's complement (all the others). The leftmost bit is regarded as having a value of −128 if you use 8 bits. As Stephan says, complementary arithmetic makes it very easy to implement both addition and subtraction using the same circuits on the chip, which would be much more difficult in S&M. I think that means two's complement is the de facto default format for all signed integer types.
It would only be possible to fit −255 into 8 bits if you make all the numbers non‑positive. In unsigned format, the smallest value would be 0.

I have heard complaints that Java® doesn't support unsigned versions of all integer primitives, but that is a different question, and I think unsigned versions aren't going to happen, ever.
 
Campbell Ritchie
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:. . .  https://en.wikipedia.org/wiki/Two%27s_complement

Wikipedia has fallen down there; it calls the leftmost bit the sign bit, which it isn't, even though it always determines the sign of the number, and it incorrectly says that two's complement is determined by inverting the bits and adding 1.
Two's complement numbers are determined by the following procedure:-
  • 1: Determine the base and number of digits. In binary that means base 2 and the number of digits is the same as the numbers of bits. You need to specify that information. It is possible to have complements in other bases than 2.
  • 2: Exactly half the range of the numbers are negative (larger numbers if unsigned) and half (smaller numbers) are non‑negative.
  • 3: Non‑negative numbers look the same as unsigned.
  • 4: For negative numbers, subtract the positive equivalent from one more than the range. In eight‑digit binary, that is 1_0000_0000.
  • Example: the two's complement of 0110_1001 is calculated from
    1_0000_0000
      0110_1001−
      1001_0111

    The fact that zero is represented only once means you have one more negative number than positive number, so the range in 8 bits is -128...127.
    Subtraction from one more, in this case from 2⁸ in binary, is how complement numbers are determined. [Not one's complement.] The flip the bits and add 1 technique always gives the same result as the above subtraction.
     
    Moussa Kheyar
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for all your responses friends,
    from your answers and the wikipedia article.
    I anderstand that  for exemple 1111 1111 realy is like below:
    (-)111 1111  
    we complement each the number witch give us:
    (-) 000 0000
    we add 1 witch give us
    (-)000 0001 = -1

    these are som negative  bytes if i am not wrong :
    1000 0000  is -((111 1111) +1)= -(127-1)=-128

    1111 0000 is -((000 1111)+1)= -(15+1) =-16

    Please confirm me if it is correct or false.
    Thanks for you friends. all my respect for all members of coderanch.
     
    Campbell Ritchie
    Marshal
    Posts: 79471
    379
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Moussa Kheyar wrote:Thanks

    That's a pleasure

    . . . 1111 1111 realy is like below:
    (-)111 1111 . . . .

    I don't understand that, but it looks incorrect. You have used the leftmost bit as a sign bit, but it has a value.
    In eight bits, 1111_1111 is (−128)+64+32+16+8+4+2+1. That gives you −1.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic