Java Program to Illustrate Use of Binary Literals
Last Updated :
30 Aug, 2024
A binary literal is a number represented in binary (0s and 1s). In Java, you can use binary literals for integral types such as byte
, short
, int
, and long
. To specify a binary literal, prefix the number with 0b
or 0B
. This allows you to define numbers in binary format directly in your code. When executed, Java will convert these binary literals to their decimal equivalents.
Implementing Binary literals in Java
Example 1:
In Java, you can use binary literals to represent integral types such as byte
, short
, int
, and long
. The following Java program demonstrates how to implement and use these binary literals effectively.
Java
// Java Program to Illustrate Use of Binary Literals
public class BinaryLiteralsDemo {
public static void main(String[] args) {
// Byte type Binary Literals
byte a1 = 0b011; // Binary 011 is decimal 3
byte a2 = 0B101; // Binary 101 is decimal 5
System.out.println("Binary Literal in Byte:");
System.out.println("a1 = " + a1 + ", a2 = " + a2);
// Short type Binary Literals
short b1 = 0b101; // Binary 101 is decimal 5
short b2 = 0B111; // Binary 111 is decimal 7
System.out.println("Binary Literal in Short:");
System.out.println("b1 = " + b1 + ", b2 = " + b2);
// Int type Binary Literals
int c1 = 0b011; // Binary 011 is decimal 3
int c2 = 0B111; // Binary 111 is decimal 7
System.out.println("Binary Literal in Integer:");
System.out.println("c1 = " + c1 + ", c2 = " + c2);
// Long type Binary Literals
long d1 = 0b0000011111100011; // Binary 0000011111100011 is decimal 2019
long d2 = 0B0000011111100001; // Binary 0000011111100001 is decimal 2017
System.out.println("Binary Literal in Long:");
System.out.println("d1 = " + d1 + ", d2 = " + d2);
}
}
OutputBinary 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
Explanation of the above Program:
- Byte Literals:
0b011
and 0B101
for byte
variables represent decimal values 3
and 5
. - Short Literals:
0b101
and 0B111
for short
variables represent decimal values 5
and 7
. - Int Literals:
0b011
and 0B111
for int
variables represent decimal values 3
and 7
. - Long Literals:
0b0000011111100011
and 0B0000011111100001
for long
variables represent decimal values 2019
and 2017
.
Example 2:
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.
Java
// Java Program to Illustrate Operations on Binary Literals
public class BinaryOperationsDemo {
public static void main(String[] args) {
byte n1 = 3; // Decimal number
byte n2 = 0b011; // Binary 011 (decimal 3)
byte n3 = -0b111; // Negative binary 111 (decimal -7)
byte n4 = 0b1101; // Binary 1101 (decimal 13)
System.out.println("n1 = " + n1); // Output: 3
System.out.println("n2 = " + n2); // Output: 3
System.out.println("n3 = " + n3); // Output: -7
System.out.println("n4 = " + n4); // Output: 13
// Operations
System.out.println("is n1 and n2 equal: " + (n1 == n2)); // Output: true
System.out.println("n2 + 1 = " + (n2 + 1)); // Output: 4
System.out.println("n3 + 1 = " + (n3 + 1)); // Output: -6
System.out.println("n4 x 2 = " + (n4 * 2)); // Output: 26
}
}
Outputn1 = 3
n2 = 3
n3 = -7
n4 = 13
is n1 and n2 equal: true
n2 + 1 = 4
n3 + 1 = -6
n4 x 2 = 26
Explanation of the above Program:
- Variable Initialization: Demonstrates binary and decimal representations.
- Operations: Shows equality checks and arithmetic operations with binary literals.
Similar Reads
Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc
5 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
Java Program to Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen
4 min read
Java Program to Print the ASCII Value ASCII is an acronym that stands for American Standard Code for Information Interchange. In this, a specific numerical value is assigned to different characters and symbols, it is a 7-bit character set containing 128 (0-127), for computers to store and manipulate, and while storing and manipulating t
5 min read
Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and
2 min read
Java Program to Convert Char to Byte Given a char in Java, the task is to write a Java program that converts this char into Byte. Examples: Input: ch = 'A' Output: 65 Input: ch = 'B' Output 66 In Java, char is a primitive data type and it is used to declare characters. It has the capability to hold 16-bit unsigned Unicode characters. T
3 min read