Skip to content

Instantly share code, notes, and snippets.

@ozkansari
Created January 6, 2013 10:49
Show Gist options
  • Save ozkansari/4466529 to your computer and use it in GitHub Desktop.
Save ozkansari/4466529 to your computer and use it in GitHub Desktop.
Sİmple binary operation example
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