Last active
August 29, 2015 14:20
-
-
Save doridori/387bdce2ed4ad2b95efe to your computer and use it in GitHub Desktop.
Java: Bitshifting Bytes post code samples
This file contains 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
byte aByte = -112; //0b1001_0000 | |
byte bByte = (byte) (aByte >> 4); //would expect 0b1111_1001 (-7) | |
System.out.println(bByte); //-7 | |
byte cByte = (byte) (aByte >>> 4); //would expect 0b0000_1001 (9) | |
System.out.println(cByte); //-7 |
This file contains 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
byte dByte = (byte) ((aByte & 0xFF) >>> 4); //we get 0b0000_1001 (9) | |
System.out.println(dByte); //9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment