Use of Binary Literals in Java



In this article, we will learn to illustrate the use of binary literals in Java. This feature is particularly useful when working with low-level data manipulation, such as in embedded systems or when dealing with bitwise operations.

What is Binary Literal?

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.

For example ?

  • 0b1001 represents the binary number 1001, which is equivalent to 9 in decimal.
  • 0B1001 is also valid and represents the same value.

Binary Literal in byte Data Type

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.

byte bt1 = 0b1001; // Using upper case 0b
byte bt2 = 0B1001; // Using lower case 0B

Example

Below is an example to display a value of byte data type in binary literals ?

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

Binary Literal in short Data Type

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.

short n1 = 0b1001; // Using upper case b0
short n2 = 0B1001; // Using lower case B0

Example

Below is an example to display a value of short data type in binary literals ?

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

Binary Literal in int Data Type

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.

int n1 = 0b1001;       // Usage of upper case b
int n2 = 0B1001;       // Usage of lower-case B

Example

Below is an example to display a value of int data type in binary literals ?

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

Binary Literal in long Data Type

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.

long n1 = 0b1001;    // Using upper case b0
long n2 = 0B1001;    // Using lower case B0

Example

Below is an example to display a value of long data type in binary literals ?

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

Mathematical Operations on Binary Literals

The following Java program shows how arithmetic 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.

Example

Below is an example of performing mathematical operations on 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

Time Complexity: The operations involving binary literals is O(1). This is because arithmetic operations like addition, subtraction, multiplication, and division are constant-time operations for fixed-size data types like byte, short, int, and long.
Space Complexity: O(1), since the variables used to store binary literals occupy a fixed amount of memory.

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: 2025-01-28T18:23:53+05:30

509 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements