Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte Literal

 
Tony Tirolese
Greenhorn
Posts: 12
Eclipse IDE Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the literal binary value for a byte type variable holding a decimal value of -128?

I have been able to do this:


But I have found that I cannot do this:


What am I missing/not understanding?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0b10000000 is an int literal that is equal to 128. That means that it is outside the range of byte, and you need to explicitly cast it to byte. There are no byte literals, only int literals that are within the range of byte.
 
Tony Tirolese
Greenhorn
Posts: 12
Eclipse IDE Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:0b10000000 is an int literal that is equal to 128. That means that it is outside the range of byte, and you need to explicitly cast it to byte. There are no byte literals, only int literals that are within the range of byte.




(I'm not arguing, just trying to clear my own confusion.)

It seems to me from my reading of available reference material that -128 should be represented in binary for an 8-bit type in Java by the value 1000000 (a one followed by seven zeros).

When I assign the value 01111111 (a zero followed by seven ones) to a byte variable, I don't get any errors and everything runs fine, with no explicit cast required:


I assumed that the unary NOT action in the use of a two's complement value to initialize the b128 required an explicit cast because of possible type promotion to integer on account of using the unary NOT operator.

 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Tirolese wrote:
I assumed that the unary NOT action in the use of a two's complement value to initialize the b128 required an explicit cast because of possible type promotion to integer on account of using the unary NOT operator.



You are way over thinking it here. Literals are compile time constants. An explicit cast of a compile time constant is a compile time constant. Just cast it to a byte and you are done. It doesn't make the code (class file) longer. There isn't any int value that gets converted to a byte. etc. A cast of a literal just tells the compiler to treat it as a byte value.

Henry
 
Tony Tirolese
Greenhorn
Posts: 12
Eclipse IDE Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the high order bit used only for determining whether a value is positive or negative?

 
Tony Tirolese
Greenhorn
Posts: 12
Eclipse IDE Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Tony Tirolese wrote:
I assumed that the unary NOT action in the use of a two's complement value to initialize the b128 required an explicit cast because of possible type promotion to integer on account of using the unary NOT operator.



You are way over thinking it here. Literals are compile time constants. An explicit cast of a compile time constant is a compile time constant. Just cast it to a byte and you are done. It doesn't make the code (class file) longer. There isn't any int value that gets converted to a byte. etc. A cast of a literal just tells the compiler to treat it as a byte value.

Henry


Thanks for your answer. I'm just trying to get a grip on how this works. What I don't understand is why I can assign the value of 127, expressed as a literal in binary format, but I can't do that for -128, when the range of values for a byte is listed as -128 to 127 inclusive.
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Tirolese wrote:Is the high order bit used only for determining whether a value is positive or negative?


I don't know. I don't have Java 7 installed to try it. Why don't you try it and tell us?




Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Tirolese wrote:Is the high order bit used only for determining whether a value is positive or negative?


Just downloaded Java 7 -- and tested it.

Yes. It looks like with binary literals, Java does use the high bit for negative -- meaning it doesn't report an out of range literal. And it is smart enough to apply the compile time constant conversion rules to it.

The Code:



The Result:

-128


Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same rules that apply for decimal literals apply to binary literals. Literal values between -128 and 127 can be assigned to bytes without casting, no matter if they are represented in decimal (-128), binary(0b1111_1111_1111_1111_1111_1111_1000_0000), hexadecimal (0xFFFFFF80) or octal (037777777600).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic