This question already has an answer here:
Promotion of byte to int or long
(1 answer)
Closed 7 years ago.
I have the following code:
int i =128;
byte b = (byte) i;
System.out.println( Integer.toBinaryString(i)); //10000000
System.out.println( Integer.toBinaryString(b)); //11111111111111111111111110000000
could someone explain why 1's were added to the left when casting from Integer to Byte and how could a byte carry more than 8 bits !?
You are calling .toBinaryString on the Integer class, so your number is treated as an Integer in any case.
The reason the second call has so many 1 is because it is a negative number. In Java, bytes are signed, so the maximum positive value is 127. By casting 128 into a byte you are actually representing -128. When you cast that small negative number into an 32 bit signed integer as you were doing, all those 1s appear at the beginning.
Related
This question already has answers here:
Understanding Java data types
(2 answers)
How to represent 11111111 as a byte in java
(2 answers)
JAVA: why binary literal for byte with negative sign is being considered as integer type?
(5 answers)
Closed 1 year ago.
byte b = 0b1111_1111;
byte c = 0b111_1111;
byte d = 0b0111_1111;
The first line causes a compilation error: incompatible types. I also tried 0xFF and decimal, the same. Thanks.
And I think byte means cannot be larger than what 8 bits hold. 0b1111_1111 is 8 bits.
All basic integer types in Java are signed. To be exact, byte, short, int and long are signed big endian two complement integers. That means that they run from - 2n-1 to 2n-1 - 1 where n is the number of bits - 8 for a byte (Byte.SIZE in Java), and therefore the range is from -128 to 127.
The number you are using is always an integer; number literals always are. However, the integer is actually the value 0x000000FF in hexadecimals. That cannot be stored as it is higher than 127. You can however do an explicit conversion; your value does consist of 8 bits after all.
byte b = (byte) 0b1111_1111
works because it simply disregards the 24 bits from the left hand side of the value. Now if you print it you will get -1 as value though as 0b1111_1111 represents 1 in 8 bit two complement form.
If you need it to represent the integer value 255 then you need to perform the following little trick:
int i = b & 0xFF;
which will actually do the following:
First it will convert b to the value 0xFFFFFFFF as integer using sign extension as all calculations default to integer, and it still thinks that b represents -1 after all.
Then it performs a bitwise AND with the value 0x000000FF (which is called a "mask"), resulting of course in the same value 0x000000FF which represents 255.
If you think this is a nuisance then you are right. Java should have used unsigned bytes, but it decided to use only one particular integer type that is signed. Maybe a bit overzealous but still a big improvement over e.g. C where you have way too many integer formats, and each of them may be represented differently on various machines.
This question already has an answer here:
Understanding narrowing primitive conversion
(1 answer)
Closed 1 year ago.
Integer Wrapper class
Integer obj = new Integer("1000");
System.out.println(obj.byteValue( )); //-24
I am not able to understand that how this output is formed. I want to understand how this "1000" in an integer is converted into "-24" in a byte. I want to know about the logic behind this.
The docs say:
Returns the value of this Integer as a byte after a narrowing primitive conversion.
which isn't particularly helpful if you don't know what a "narrowing primitive conversion" is. Well, you can look into the Java Language Specification (section 5.1.3) for the definition of that:
A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.
The Integer 1000 is represented by 32 bits:
00000000 00000000 00000011 11101000
byte is 8 bits, so we discard all but the 8 lowest order bits, we get:
11101000
which is -24 in 8-bit two's complement binary. -24 = -128 + 64 + 32 + 8
Casting an integer to a byte will give the last 8 bits of of the integer.
1000 in decimal -> 1111101000 in binary
Converting this to a byte value gives you 11101000 which is -24.
This question already has answers here:
How are integers cast to bytes in Java?
(8 answers)
Closed 6 years ago.
I have the following code:
System.out.println(0b11111111);
System.out.println((byte) 0b11111111);
First row prints
255
and the second row prints
-1
Please, can someone explain me why the results are different? How does (byte) casting change 0b11111110 number that it becomes -1?
Thank you!
The range of the byte type is -128 to 127. Since 255 is not a valid byte value, the binary number 11111111 is -1 when cast to byte.
If you don't cast 0b11111111 to byte, it remains (by default) an int, and 255 is a valid int value.
This question already has answers here:
Assigning int to byte in java?
(5 answers)
Closed 8 years ago.
I have an int variable in java whose value is less than 256. So Can I store it in a Single byte variable.
I performed the following code.
int i =247;
byte b = (byte) i ;
But When I print it,
System.out.println(" i = "+(int)b);
It outputs
i = -9
Is it possible to convert int values less than 256 to single byte variables.
Pls. Help..
This isn't going to work. The MAX_VALUE for byte is 127 because bytes are signed in Java. (Consequently, the MIN_VALUE is -128. This is your typical Two's Compliment pattern.)
That being said, you have a couple options:
Leave it as an int and just use an int for your calculations. This might even be faster for you.
Leave it as a byte even if the number isn't correct. The numbers might not be correct in Java, but if all you care about is the bits being correct, they will be correct. So you can send that bite (which is a -9 in Java) across a webservice to another app that reads it as an unsigned byte, and it will read it as 247 correctly.
This question already has answers here:
What is the difference between Short and Character apart from processing?
(3 answers)
Closed 3 years ago.
According to the Java standard the short and char types both use 2 bytes so when I code something like:
char ch = 'c';
short s = ch;
There is an error saying "possible loss of precision". What am I missing here?
char is unsigned, short is signed.
So while they are both 2-byte long, they use the sixteenth bit for different purposes.
The range of the char type is 0 to 2^16 - 1 (0 to 65535).
The short range is -2^15 to 2^15 - 1 (−32,768 to 32,767).
The difference is that char is unsigned, short is signed. Thus, half the range of values of char is too big to be represented as a short (and of course, in symmetry, char cannot represent any of the negative values short can).