Java Program to Illustrate Use of Binary Literals
A binary literal is a number that is represented in 0s and 1s (binary digits). Java allows you to express integral types (byte, short, int, and long) in a binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value. So, we can express binary digits in a program by assigning them to variables, and the output of those variables after executing the program will be decimal digits.
Implementing Binary literals in Java:
We can explore the ways in which we can implement and use binary literals in Java programming language. The first and following Java program is an example to implement different types of binary literals. As mentioned above Java allows us to express byte, short, int, and long types of binary literals.
Binary Literal in Byte-----> a1 = 3, a2 = 5 Binary Literal in Short-----> b1 = 5, b2 = 7 Binary Literal in Integer-----> c1 = 3, c2 = 7 Binary Literal in Long-----> d1 = 2019, d2 = 2017
In Java, operators can be used on binary literals to perform operations. The following Java program is an example to implement different mathematical and comparison operations on binary literals. The results of these operations on binary literals are decimal digits.
n1 = 3 n2 = 3 n3 = -7 n4 = 13 is n1 and n2 equal: true n2 + 1 = 4 n3 + 1 = -6 n4 x 2 = 26