How to declare binary numbers in Java
Make use of binary literals to make your code readable. The following code segment demonstrates the use of binary literals:
int bin1 = 0b1100;
short bin2 = 0B010101;
short bin3 = (short) 0b1001100110011001;
System.out.println(bin1);
System.out.println(bin2);
System.out.println(bin3);
This will result in the following output:
12
21
-26215
How It Works
Binary literals became part of the Java language with the release of Java 7. The types byte, short, int, and long can be expressed using the binary number system. This feature can help to make binary numbers easier to recognize in code. In order to use the binary format, simply prefix the number with 0b or 0B.