Binary Literals

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.         // Binary literal in short type
  10.         short s1 = 0b101;   // Using b0, The b can be lower or upper case
  11.         short s2 = 0B101;   // Using B0
  12.         System.out.println(“———-Binary Literal in Short—————-“);
  13.         System.out.println(“s1 = “+s1);
  14.         System.out.println(“s2 = “+s2);
  15.         // Binary literal in int type
  16.         int i1 = 0b101;     // Using b0, The b can be lower or upper case
  17.         int i2 = 0B101;     // Using B0
  18.         System.out.println(“———-Binary Literal in Integer—————-“);
  19.         System.out.println(“i1 = “+i1);
  20.         System.out.println(“i2 = “+i2);
  21.         // Binary literal in long type
  22.         long l1 = 0b0000011111100001;   // Using b0, The b can be lower or upper case
  23.         long l2 = 0B0000011111100001;   // Using B0
  24.         System.out.println(“———-Binary Literal in Long—————-“);
  25.         System.out.println(“l1 = “+l1);
  26.         System.out.println(“l2 = “+l2);
  27.     }
  28. }

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
Next TopicJava Switch with String
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

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. […]

Tags:

....
...