Skip to content

Instantly share code, notes, and snippets.

@doridori
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doridori/387bdce2ed4ad2b95efe to your computer and use it in GitHub Desktop.
Save doridori/387bdce2ed4ad2b95efe to your computer and use it in GitHub Desktop.
Java: Bitshifting Bytes post code samples
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
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