Java Binary Literals

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Binary Literals

Java added a new feature Binary Literal in Java 7. I allows you to express integral types (byte, short, int, and long) in binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value.

In the following example, we are creating binary literals from integral values.

Binary Literal Example

  1. public class BinaryLiteralsExample {  
  2.     public static void main(String[] args) {  
  3.         // Binary literal in byte type  
  4.         byte b1 = 0b101;    // Using b0, The b can be lower or upper case  
  5.         byte b2 = 0B101;    // Using B0  
  6.         System.out.println("----------Binary Literal in Byte----------------");  
  7.         System.out.println("b1 = "+b1);  
  8.         System.out.println("b2 = "+b2);  
  9.           
  10.         // Binary literal in short type  
  11.         short s1 = 0b101;   // Using b0, The b can be lower or upper case  
  12.         short s2 = 0B101;   // Using B0  
  13.         System.out.println("----------Binary Literal in Short----------------");  
  14.         System.out.println("s1 = "+s1);  
  15.         System.out.println("s2 = "+s2);  
  16.           
  17.         // Binary literal in int type  
  18.         int i1 = 0b101;     // Using b0, The b can be lower or upper case  
  19.         int i2 = 0B101;     // Using B0  
  20.         System.out.println("----------Binary Literal in Integer----------------");  
  21.         System.out.println("i1 = "+i1);  
  22.         System.out.println("i2 = "+i2);  
  23.           
  24.         // Binary literal in long type  
  25.         long l1 = 0b0000011111100001;   // Using b0, The b can be lower or upper case  
  26.         long l2 = 0B0000011111100001;   // Using B0  
  27.         System.out.println("----------Binary Literal in Long----------------");  
  28.         System.out.println("l1 = "+l1);  
  29.         System.out.println("l2 = "+l2);  
  30.     }  
  31. }  

Output:

----------Binary Literal in Byte----------------
b1 = 5
b2 = 5
----------Binary Literal in Short----------------
s1 = 5
s2 = 5
----------Binary Literal in Integer----------------
i1 = 5
i2 = 5
----------Binary Literal in Long----------------
l1 = 2017
l2 = 2017

Binary Literal Example 2

In this example, we are creating negative binary, using underscore in binary literals and manipulating as well.

  1. public class BinaryLiteralsExample {  
  2.     public static void main(String[] args) {  
  3.         byte b1 = 5// a decimal value  
  4.         // Using binary of 5  
  5.         byte b2 = 0b101;    // using b0, The b can be lower or upper case  
  6.         // Declaring negative binary  
  7.         byte b3 = -0b101;  
  8.         // Using underscore in binary literal  
  9.         byte b4 = 0b101_0;  
  10.         System.out.println("b1 = "+b1);  
  11.         System.out.println("b2 = "+b2);  
  12.         System.out.println("b3 = "+b3);  
  13.         System.out.println("b4 = "+b4);  
  14.         // Check whether binary and decimal are equal   
  15.         System.out.println("is b1 and b2 equal: "+(b1==b2));  
  16.         // Perform operation on binary value  
  17.         System.out.println("b2 + 1 = "+(b2+1));  
  18.         // Perform operation on negative binary value  
  19.         System.out.println("b3 + 1 = "+(b3+1));  
  20.         System.out.println("b4 x 2 = "+(b4*2));  
  21.     }  
  22. }  

Output:

b1 = 5
b2 = 5
b3 = -5
b4 = 10
is b1 and b2 equal: true
b2 + 1 = 6
b3 + 1 = -4
b4 x 2 = 20