File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bit Shifting and overflow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bit Shifting and overflow" Watch "Bit Shifting and overflow" New topic
Author

Bit Shifting and overflow

babudev Yam
Greenhorn

Joined: Jul 27, 2006
Posts: 8
Hi,

What happens when we cast a int with a byte and the number we are casting exceeds the byte range.

byte b=(byte)255 will result in -1 (I tried running it thru the programme)

but how did it become -1? Can anyone explain that to me?

I know basic thing that bits are dropped when we narrow them but I want to know the algorithm or logic used for dropping.

Thanks
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9426
    
    2

Hi above,

For example when you say byte b = (byte)128 which is out of range without a cast, what it does is, it starts assigning from the negative limit i.e., after byte b = (byte)128, b will be -128. I dont know the logic behind. Can anyone please explain the undelying logic behind this?

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
alamelu vr
Greenhorn

Joined: Oct 11, 2006
Posts: 1
Hi

The algorithm is simple...

1)The integer is represented in binary....

2)then converted into byte by taking last 8 bits..

3)The last 8 bits is then represented in decimal....

for example

255 in binary is 11111111

Since the range of byte is from -128 to 127... when you convert the binary number 11111111 to decimal...you should take it in 2's complement form...and the resultant decimal number is -1.


Another example

300 in binary is 100101100

and the last 8 digits is 00101100

and when you convert its 44
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Jothi Shankar Kumar Sankararaj wrote:
i.e., after byte b = (byte)128, b will be -128. I dont know the logic behind. Can anyone please explain the undelying logic behind this?

Good news:
This will not be on the exam.

Bad news:
It is a bit complicated

How is +127 represented in a byte?
as 64+32+16+8+4+2+1:



127=1111111 seven bits, the first bit is for the sign, plus is 0 and minus is 1.


Piece of cake.

If you want to know what happens, if you add one to the 127, you got to know, how negative numbers are represented:

By a Two's complement (see wikipedia or read on)
The Two's complement is made as follows:
first invert all bits:


second add one:


so 1 0 0 0 0 0 0 1 represents -127


If the first bit is negative, you know it is a negative number
to calculate negative numbers to positive ones, do the same thing and in the same direction (invert all bits, then add one).
It would sound more logical if you first substract one and then invert all bits, but as for the two's complement...

... this makes no difference ;-)


back to your question:
what happens if you add 1 to 127 and store it into a byte?

imagine you add two int literals (byte b = (byte)(127 + 1);



if you store this in a byte, all left 24 bits are cut off and you get:


10000000


From the first bit = 1 you know it is a negative number. the positive representation would be
first invert all bits,
second, add one

so:



oops! now we're stuck!

The largest possible negative number (also called "weird number") gives us some trouble here. Because the largest possible negative number (-128 in case of a byte) is the one and only exception to the rule that inversion of all bits plus one makes a negative number out of a positive one (and vice versa).


To show, that 1000 0000 really represents -128 you can show with a trick:
We already know, that 1000 0001 represents minus one two seven (see above). If we just substract one, it gives us


or, more formal:

add minus one. what is minus one?




Add them:

The leading one is just cut of (overflow) as there are only eight bits in a byte (I know you remembered that!).

result 1000 0000 = minus 128


So the bit pattern 1000 0000 represents -128 (if it's a byte)
if it's a short:
0000 0000 1000 0000 represents just +128

But now at this point I'm totally disturbed and have forgotten what this thread was all about and what was the question.
Sorry!

Yours,
Bu.
[ December 04, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi cowboys,

back to the original question:

babudev Yam wrote:
byte b=(byte)255 will result in -1
Can anyone explain that to me?



The 255 is an integer literal represented by


If you store this into a byte, you'll get


The leading one indicates a negative number. If you perform the Two's complement:




You'll see, that 1111 1111 makes minus one.


Yours,
Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bit Shifting and overflow