Created
January 6, 2013 10:54
-
-
Save ozkansari/4466544 to your computer and use it in GitHub Desktop.
Java bitwise opeartions example
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
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