Created
October 8, 2015 05:27
-
-
Save mrleolink/a81d3bf2e490d2c678a5 to your computer and use it in GitHub Desktop.
Unsigned right shift operator explanation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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