aspose file tools*
The moose likes Beginning Java and the fly likes Casting an int to a byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting an int to a byte" Watch "Casting an int to a byte" New topic
Author

Casting an int to a byte

John Ryan
Ranch Hand

Joined: Mar 14, 2001
Posts: 124
Hi all,
I am running a simple test to see what happens when i cast an int with a value of -129 into a byte.
My understanding is that when narrowing into an integral type that only the n lowest order bits are used where n is the size of the type to which you are casting. This means when casting to a byte that only the lowest 8 bits are considered.
So an integer of value -129 would have 1 set at position 31. However as +129 is 10000001 in binary i persume that these would be the lowest 8 bits.
To cast this into a byte the 8 bit is 1 so it would become a negative byte. The bits would then be flipped resulting in 11111110 being stored in the type. The 7 bits that hold the value would indicate a value of 126 ( as 01111110 is +126 ). As the number is stored in two-complement 1 is added to the value stored so i would expect that the overall value stored in the byte would be -127.
However my test program tells me that the cast results in a value of +127. Any idea why? Which part of my assumptions/understanding is incorrect?
thanks,
John
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi john,
here is what happens,
+129 = 00000000 00000000 00000000 10000001
i.e.
-129 = 11111111 11111111 11111111 01111111
(this is using 2's complement)
to get a 2's complement of any binary number,
1. you have to flip all bits
2. you have to add 1 to the result
and thats what i have done to get -129 from +129.
now when you cast this to byte the lower 8 bits that are 01111111 will be considered. what is the value for 01111111? its +127, right??
thats it. there is no further 2's complement or anything else performed. you get lower byte which has +127 value.
remember following about casting,
its not a magic. its a logic. it doesnt remember what the orginal value's sign was or anything. IT JUST CUTS (in case of downcasting of course) BITS AND RETURNS WHATEVER IS LEFT.
hope this was helpful.
regards
maulin
John Ryan
Ranch Hand

Joined: Mar 14, 2001
Posts: 124
Originally posted by Maulin Vasavada:
hi john,
here is what happens,
+129 = 00000000 00000000 00000000 10000001
i.e.
-129 = 11111111 11111111 11111111 01111111
(this is using 2's complement)
to get a 2's complement of any binary number,
1. you have to flip all bits
2. you have to add 1 to the result
and thats what i have done to get -129 from +129.
now when you cast this to byte the lower 8 bits that are 01111111 will be considered. what is the value for 01111111? its +127, right??
thats it. there is no further 2's complement or anything else performed. you get lower byte which has +127 value.
remember following about casting,
its not a magic. its a logic. it doesnt remember what the orginal value's sign was or anything. IT JUST CUTS (in case of downcasting of course) BITS AND RETURNS WHATEVER IS LEFT.
hope this was helpful.
regards
maulin

Thanks Maulin. It all clear now. My error was in forgettin to apply two's complement when storing -129 in the int.
Cheers,
John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting an int to a byte