This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.

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

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

posted

0

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