Home » Java Binary Literals

Java Binary Literals

by Online Tutorials Library

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

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.

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 

You may also like