# Complete J2 Cert & unsigned right shift

Kelley Koszegi

Greenhorn

Posts: 27

posted 15 years ago

Hello,

I am viewing an example shown in the 2nd edition of the Complete Java2 Certification Study guide, p. 51. In this example, they are attempting to demonstrate the unsigned right shift of a byte. The example shows -64 >>> 4. They show the binary representation of -64 to be '11000000'. I am confused. This is not how I thought -64 should be represented. Aren't all negative numbers in 2's complement? So 64 = '01000000', inverted to '10111111' and then adding 1 would make it '11111110'? Is my math incorrect?

I have checked the book's errata and also the forum archive. Can someone clarify it for me?

Thanks,

--kkoszegi

I am viewing an example shown in the 2nd edition of the Complete Java2 Certification Study guide, p. 51. In this example, they are attempting to demonstrate the unsigned right shift of a byte. The example shows -64 >>> 4. They show the binary representation of -64 to be '11000000'. I am confused. This is not how I thought -64 should be represented. Aren't all negative numbers in 2's complement? So 64 = '01000000', inverted to '10111111' and then adding 1 would make it '11111110'? Is my math incorrect?

I have checked the book's errata and also the forum archive. Can someone clarify it for me?

Thanks,

--kkoszegi

Kelley Koszegi<br />Sun Certified Java Programmer

Dave Vick

Ranch Hand

Posts: 3244

posted 15 years ago

Kelley

It looks like they have just shortened it, remember that ints are always 4 bytes,it also looks like your math is wrong too - or your just typed it in wrong.

64 would be:

00000000 00000000 00000000 01000000

flip the bits:

11111111 11111111 11111111 10111111

add 1

11111111 11111111 11111111 11000000

Using only one byte is often the biggest problem people have with the bit operators, it is especially important when you start using negative numbers.

hope that helps

------------------

Dave

Sun Certified Programmer for the Javaï¿½ 2 Platform

It looks like they have just shortened it, remember that ints are always 4 bytes,it also looks like your math is wrong too - or your just typed it in wrong.

64 would be:

00000000 00000000 00000000 01000000

flip the bits:

11111111 11111111 11111111 10111111

add 1

11111111 11111111 11111111 11000000

Using only one byte is often the biggest problem people have with the bit operators, it is especially important when you start using negative numbers.

hope that helps

------------------

Dave

Sun Certified Programmer for the Javaï¿½ 2 Platform

Dave

Corey McGlone

Ranch Hand

Posts: 3271

sing

Ranch Hand

Posts: 121

Corey McGlone

Ranch Hand

Posts: 3271

posted 15 years ago

No. In binary, 1 + 1 does not equal 0, it equals 10. Like this:

Since this generates a one in the next position, you must add that just as if you were doing base 10 math, like this:

Why is there a 2 in the tens place? That's because we had to carry a 1 from the one's place. The same holds true in binary math. In this case, we're doing this:

The 1+1 in the first position creates a 10. We drop the 0 down and carry the 1 to the next position. This creates another situation where we have 1+1 which generates yet another 10. Once again, we drop the 0 down and carry the one. This continues until we reach the second position from the left. In that case we add the carried 1 to 0, which leaves 1. Therefore, we can just drop the 1 down, carry nothing, and continue on to the last position.

I hope my explanation makes sense to you, but the way to carry numbers in binary addition is identical to the way you carry numbers in base 10 addition - the only difference is that now we're only using 1's and 0's.

Corey

Can you explain how's this work? Thank You.

10111111

+ 00000001

------------

11000000

Why 1 ?

+ 1

----

0

No. In binary, 1 + 1 does not equal 0, it equals 10. Like this:

Since this generates a one in the next position, you must add that just as if you were doing base 10 math, like this:

Why is there a 2 in the tens place? That's because we had to carry a 1 from the one's place. The same holds true in binary math. In this case, we're doing this:

The 1+1 in the first position creates a 10. We drop the 0 down and carry the 1 to the next position. This creates another situation where we have 1+1 which generates yet another 10. Once again, we drop the 0 down and carry the one. This continues until we reach the second position from the left. In that case we add the carried 1 to 0, which leaves 1. Therefore, we can just drop the 1 down, carry nothing, and continue on to the last position.

I hope my explanation makes sense to you, but the way to carry numbers in binary addition is identical to the way you carry numbers in base 10 addition - the only difference is that now we're only using 1's and 0's.

Corey