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.

  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
advertisement

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.