• 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

byte 0x7f, 0x80, 0x81

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why does it not accept 0x80, 0x81 when it does for 7F?
(Is it not only a literal well in the range.)

Why does -0x80 not cause any problems?
and why is -0x81 not accepted?
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..

byte(8 bits) range --> 2^(8-1) to (2^(8-1)) -1 whis is equal to
-128 to 127

Note : we only use 7 bits because one bit is use for sign(+ or -).
Thats why we calculate the range by number of bits a certain type can hold minus 1.
And (2^(8-1)) -1 --> -1 because 0 is count as positive number.


Therefore if you put 0x80(128 in decimal) without casting then it won;t compile because it does not fit to byte. byte can't hold more than 127 except you cast it.(but see the effects of casting).

Hope it helps.
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hold it.
You were using the calculator I think.
0x80 is the value of Byte.MIN_VALUE in hexadecimal so my question is still valid and I need a different explanation.

Benny I suggest u look at this url for
the number system. I think I got it all right there.
[ June 22, 2004: Message edited by: Swamy Nathan ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0x80 is not seen at -128. Rather, it is seen as an int of value 128 as all literals are ints. At that point, the compiler checks to see if 128 will fit into a byte. Of course, it won't as the range of a byte goes only to 127. Therefore, you get a compiler error.

Similary, -0x80 works because this is seen as -128, which does fit into a byte.
 
Beny Na
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did not use any calculator..
0x80 is simple to count by/without hand as 8*(16^1) = 128 in decimal and the range for byte is -128 to 127 therefore 0x80 will not fit into byte.
that my thinking before replying , i will used calculator for big calculation such 16^6
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK so Benny was right somehow.
What I think you guys are telling me is that the literal 0x80 as a literal is an int. and is parsed into an int before it is narrowed into the byte.

I got shot there. Let me see if I can figure out properly what u said. Am a bit slow right now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic