1

I am trying to figure out the difference between >>= and >>>=. I understand what they do, but i don't understand the difference. The following has an output of 38 152 38 152. The bit-wise assignment >>>= seems to be doing exactly the same as >>=.

public static void main(String[] args)
{
    int c = 153;
    System.out.print((c >>= 2));
    System.out.print((c <<= 2));
    System.out.print((c >>>= 2));
    System.out.print((c <<= 2));
}
CrazedCoder
  • 294
  • 3
  • 14
  • 4
    `I understand what they do` What do they do? 100% of your grade depends on your answer. – Sotirios Delimanolis Jun 25 '14 at 20:40
  • You can find the difference between `>>=` and `>>>=` (and between `>>` and `>>>`) when you attempt these operators on negative numbers. – rgettman Jun 25 '14 at 20:42
  • 1
    Explanation, including the difference between `>>` and `>>>`, here: [Bitwise and Bit Shift Operators](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html) – Jesper Jun 25 '14 at 20:42
  • 1
    Tale a look at [Absolute Beginner's Guide to Bit Shifting](http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting). Also [Difference between >>> and >>](http://stackoverflow.com/questions/2811319/difference-between-and) – Pshemo Jun 25 '14 at 20:42

1 Answers1

6

Read more about Bitwise and Bit Shift Operators

>>      Signed right shift
>>>     Unsigned right shift

The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator >>> shifts a zero into the leftmost position,

while the leftmost position after >> depends on sign extension.

In simple words >>> always shifts a zero into the leftmost position whereas >> shifts based on sign of the number i.e. 1 for negative number and 0 for positive number.


For example try with negative numbers.

int c = -153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.println(Integer.toBinaryString(c <<= 2));

c = 153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));

output:

11111111111111111111111111011001
11111111111111111111111101100100
  111111111111111111111111011001
11111111111111111111111101100100
                          100110
                        10011000
                          100110
                        10011000
Braj
  • 46,415
  • 5
  • 60
  • 76
  • It might be clearer if, rather than using `System.out.println(x)` (for some x), you used `System.out.printf("%32d%n", x)` to get them right-justified, although I'm not sure if it would. – David Conrad Jun 25 '14 at 20:55
  • 1
    @DavidConrad Thanks but I want to display it in binary format as well to make it more clear. I have updated it in my post. – Braj Jun 25 '14 at 20:59
  • 1
    Oh, yes, sorry, I meant to write `%32s`. Brain fart. – David Conrad Jun 25 '14 at 21:10