Skip to content

Instantly share code, notes, and snippets.

@ozkansari
Created January 6, 2013 10:54
Show Gist options
  • Save ozkansari/4466544 to your computer and use it in GitHub Desktop.
Save ozkansari/4466544 to your computer and use it in GitHub Desktop.
Java bitwise opeartions example
class BitwiseShiftOperations {
/**
* Shift Operations Example
*/
public static void main(String[] args) {
int val = 0x000F;
printBinary(val);
System.out.println("<< 1 (Signed left shift)");
val = val << 1;
System.out.println("-----------------");
printBinary(val);
System.out.println(">> 2 (Signed right shift)");
val = val >> 2;
System.out.println("-----------------");
printBinary(val);
System.out.println(">>> 1 (Unsigned right shift)");
val = val >>> 1;
System.out.println("-----------------");
printBinary(val);
}
public static void printBinary(int val) {
System.out.print(("00000000" + Integer.toBinaryString(val) ).substring(Integer.toBinaryString(val).length()) );
System.out.println(" ( Hex: 0x" + Integer.toHexString(val) + ", Octal: " + val + ")");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment