Java Program to Illustrate Use of Binary Literals

«
»
This is a Java Program to Illustrate Use of Binary Literals.A literal is an explicit value. To specify a binary literal, add the prefix 0b or 0B to the number. For example : int number=123;
The value 123 is a literal. Since it’s a normal base-10 value, we might say it is a decimal literal.

We use binary literals with different type of datatypes like byte, short and int to illustrate its use. We also use addition operation to add two binary literals.

Here is the source code of the Java Program to Illustrate Use of Binary Literals. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. import java.util.Scanner;
  2. public class Binary_Literal
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         byte aB = 0b00100001;
  7.         short aS = 0b10100010100;
  8.         int a1 = 0b10110;
  9.         int a2 = 0b101;
  10.         int a3 = 0b1011; 
  11.         int aI=a2+a3;
  12.         System.out.println("Byte value:"+aB);
  13.         System.out.println("Short value:"+aS);
  14.         System.out.println("Integer value:"+a1);
  15.         System.out.println("Result:"+aI);
  16.     }
  17. }

Output:

$ javac Binary_Literal.java
$ java Binary_Literal
 
Byte value:33
Short value:1300
Integer value:22
Result:16

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn