Java Program to Illustrate Use of Binary Literals


A binary literal is a number that is denoted using binary digits that are 0s and 1s. The values written in data types – byte, int, long, and short can be easily expressed in a binary number system.

The prefix 0b or 0B is added to the integer to declare a binary literal.

Let us see some examples to understand the topic in a better way.

Example

The following program displays a value of byte data type to which the value of binary literal is assigned. A class named BinaryLiteral1 is created within which 2-byte data type variables are declared and assigned binary literal values and the same gets displayed.

public class BinaryLiteral1 {
   public static void main(String[] args) {
      // Binary literal in a byte type
      byte bt1 = 0b1001; // Using upper case 0b
      byte bt2 = 0B1001; // Using lower case 0B
      System.out.println("Illustrating the usage of Binary Literal in Byte data type");
      System.out.println("Value of variable bt1 = "+bt1);
      System.out.println("Value of variable bt2 = "+bt2);
   }
}

Output

Illustrating the usage of Binary Literal in Byte data type
Value of variable bt1 = 9
Value of variable bt2 = 9

Example

The following program displays a value of short data type to which the value of binary literal is assigned. A class named BinaryLiteral2 is created within which 2 short data type variables are declared and are assigned binary literal values and the same gets displayed.

public class BinaryLiteral2 {
   public static void main(String[] args) {
      // Binary literal in short type
      short n1 = 0b1001; // Using upper case b0
      short n2 = 0B1001; // Using lower case B0
      System.out.println("Illustrating the usage of Binary Literal in short data type");
      System.out.println("The value of variable n1 = "+n1);  
      System.out.println("The value of variable n2 = "+n2);   
   }
}

Output

Illustrating the usage of Binary Literal in short data type
The value of variable n1 = 9
The value of variable n2 = 9

Example

The following program displays a value of the int data type to which the value of binary literal is assigned. A class named BinaryLiteral3 is created within which 2 int data type variables are declared and are assigned binary literal values and the same gets displayed.

public class BinaryLiteral3 {
   public static void main(String[] args) {
      // Binary literal in int type
      int n1 = 0b1001; 
      // Usage of upper case b
      int n2 = 0B1001; 
      // Usage of lower-case B
      System.out.println("Illustrating the usage of Binary Literal in int data type");
      System.out.println("The value of variable st1 = "+n1);  
      System.out.println("The value of variable st2 = "+n2);  
       
    }
}

Output

Illustrating the usage of Binary Literal in int data type
The value of variable st1 = 9
The value of variable st2 = 9

Example

The following program displays a value of long data type to which the value of binary literal is assigned. A class named BinaryLiteral4 is created within which 2 long data type variables are declared and are assigned binary literal values and the same gets displayed.

public class BinaryLiteral4 {
   public static void main(String[] args) {
      // Binary literal in long type
      long n1 = 0b1001;    // Using upper case b0
      long n2 = 0B1001;    // Using lower case B0
      System.out.println("Illustrating the usage of Binary Literal in long data type");
      System.out.println("The value of variable n1 = "+n1);  
      System.out.println("The value of variable n2 = "+n2);  
       
   }
}

Output

Illustrating the usage of Binary Literal in long data type
The value of variable n1 = 9
The value of variable n2 = 9

Example

The following Java program shows how mathematical operations are performed on binary literals. A class named BinaryLiteral5 is created within which different mathematical operations are performed on positive and negative binary literals.

public class BinaryLiterals5 {  
   public static void main(String[] args) {
      // Declaring a decimal value 
      byte n1 = 26;  
      // Declaring a positive binary literal
      byte n2 = 0b1001;      
      // Declaring a negative binary literal
      byte n3 = -0b1001;  
      // Declaring a negative binary literal Using an underscore 
      byte n4 = -0b1001_0;  
      // Declaring a positive binary literal using an underscore 
      byte n5 = 0b1001_00;  
      // Declaring a positive binary literal using an underscore 
      byte n6 = 0b101_00;  
      //displaying the values of above declared variables
      System.out.println(" The value of variable n1 = "+n1);  
      System.out.println("The value of variable n2 = "+n2);  
      System.out.println("The value of variable n3 = "+n3);  
      System.out.println("The value of variable n4 = "+n4);  
      System.out.println("The value of variable n5 = "+n5);
      System.out.println("The value of variable n6 = "+n6);

      // Check if the binary values present in n2 and n3 are equal   
      System.out.println(" Are the values of n2 and n3 same: "+(n2 == n3));  
      // Performing mathematical operations on binary values  
      System.out.println("n2 + 1 = "+(n2 + 1));  
      System.out.println("n2 - 1 = "+(n2 - 1));  
      System.out.println("n3 * 2 = "+(n3 * 2));  
      System.out.println("n3 / 2 = "+(n5 / n4)); 
      System.out.println("n5 - n6 = "+(n5 - n6));  
   }
}  

Output

The value of variable n1 = 26
The value of variable n2 = 9
The value of variable n3 = -9
The value of variable n4 = -18
The value of variable n5 = 36
The value of variable n6 = 20
 Are the values of n2 and n3 same: false
n2 + 1 = 10
n2 - 1 = 8
n3 * 2 = -18
n3 / 2 = -2
n5 - n6 = 16

Conclusion

This article threw light on the usage of Binary Literals. Programs to display values written in the byte, short, int, and long data types respectively in binary literals have been discussed in this article. Further, an implementation to show how different mathematical operations are performed on binary literals has been discussed here.

Updated on: 12-Apr-2023

309 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements