Skip to content

Instantly share code, notes, and snippets.

@mrleolink
Created October 8, 2015 05:27
Show Gist options
  • Save mrleolink/a81d3bf2e490d2c678a5 to your computer and use it in GitHub Desktop.
Save mrleolink/a81d3bf2e490d2c678a5 to your computer and use it in GitHub Desktop.
Unsigned right shift operator explanation
int value = -229;
System.out.println(value);
System.out.println(value >> 5);
System.out.println(value >>> 5);
System.out.println(Integer.toBinaryString(value));
System.out.println(Integer.toBinaryString(value >> 5)); // `>>` preserves integer's sign
System.out.println(Integer.toBinaryString(value >>> 5)); // `>>>` doesn't preserve integer's sign, just shift everything to the right then fill all the empty bit on the left with 0
//======================= RESULT
-229
-8
134217720
11111111111111111111111100011011
11111111111111111111111111111000 // still negative number (-8)
111111111111111111111111000 // equals to 00000111111111111111111111111000 -> a positive number (134217720)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment