Created
January 6, 2013 10:49
-
-
Save ozkansari/4466529 to your computer and use it in GitHub Desktop.
Sİmple binary operation 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 BitDemo1 { | |
/** | |
* prints "2" | |
*/ | |
public static void main(String[] args) { | |
int val = 0x2222; | |
int bitmask = 0x000F; | |
printBinary(val); | |
System.out.println("&"); | |
printBinary(bitmask); | |
System.out.println("------------"); | |
printBinary(val & bitmask); | |
} | |
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