Compute a logical right shift AB in Java where A and B are i
Compute a logical right shift (A>>>B) in Java where A and B are ints, and (A>>B) in C++ where A and B are unsigned ints.
Convert the following base 10 numbers to 16-bit two’s complement. Show your work.
Solution
A = 1111 0000 0000 0000 0000 0000 1100 0011 0000
B = 0000 0000 0000 0000 0000 0000 0000 0000 0101
In JAVA
In JAVA >>> is treated as unsigned right shift
This operator shifts each bits of A , B number of times towards right. And at each shift the MSB of A is set to 0.
So here B=5
A= 1 111 0000 0000 0000 0000 0000 1100 0011 0000
As sign bit is 1, so A is a negative number. So represent it in 2’s complement form,
2’s complement of A= 1’s complements of A +1
I,e = 1 000 1111 1111 1111 1111 1111 0011 1101 0000
Now after 1st shift: 0 100 0111 1111 1111 1111 1111 1001 1110 1000
After 2nd shift : 0 010 0011 1111 1111 1111 1111 1100 1111 0100
After 3rd shift : 0 001 0001 1111 1111 1111 1111 1110 0111 1010
After 4th shift : 0 000 1000 1111 1111 1111 1111 1111 0011 1101
After 5th shift : 0 000 0100 0111 1111 1111 1111 1111 1001 1110
Now A = 0 000 0100 0111 1111 1111 1111 1111 1001 1110 (ans)
As sign bit is 0 .It is a positive number . value remains same
In C++
In C++ >> is treated as signed right shift
This operator shifts each bits of A , B number of times towards right. And at each shift the MSB of A is set to 0 if the number is +ve and 1 if the number is negative.
So here B=5
A= 1 111 0000 0000 0000 0000 0000 1100 0011 0000
As sign bit is 1, so A is a negative number. So represent it in 2’s complement form,
2’s complement of A= 1’s complements of A +1
I,e
= 1 000 1111 1111 1111 1111 1111 0011 1101 0000
Now after 1st shift : 1 100 0111 1111 1111 1111 1111 1001 1110 1000
After 2nd shift : 1 110 0011 1111 1111 1111 1111 1100 1111 0100
After 3rd shift : 1 111 0001 1111 1111 1111 1111 1110 0111 1010
After 4th shift : 1 111 1000 1111 1111 1111 1111 1111 0011 1101
After 5th shift : 1 111 1100 0111 1111 1111 1111 1111 1001 1110
Now A = 1 111 1100 0111 1111 1111 1111 1111 1001 1110
As sign bit is 1 .It is a negative number , To find actual value, find 2’s complement of above
I,e 1 000 0011 1000 0000 0000 0000 0000 0110 0001
+ 0 000 0000 0000 0000 0000 0000 0000 0000 0001
----------------------------------------------------------------------
= 1 000 0011 1000 0000 0000 0000 0000 0110 0010 (ans)
-------------------------------------------------------------------------------------------------------------------------------------------
Convert (121)10 to 16 bit 2’s complement
2’s complement of a number = 1’s complement of that number + 1
121 : 0 000 0000 0111 1001 // bold and underlined bit is sign bit
1’s Complement : 0 111 1111 1000 0110
+ 1
---------- ----------------------------
2’s Complement : 0 111 1111 1000 0111 (ans)
------------------------------------------------------------------------------------------------------------------------------------------
Convert (-99)10 to 16 bit 2’s complement
-99 : 1 000 0000 0110 0011 // bold and underlined bit is sign bit
1’s Complement : 1 111 1111 1001 1100
+ 1
---------- ----------------------------
2’s Complement : 1 111 1111 1001 1101 (Ans)

