728x90 AdSpace

Saturday, 28 December 2013

Java7 – Binary Literals

In this tutorials we are going to see the integral types (byte, short, int, and long ) in  java7.This tutorials is created by referring Oracle official web site.Some content is taken from there. 
In Java7, the integral types (byte, short, int, and long ) can also expressed using binary number system.To specify binary literal, add prefix 0b or 0B to the number.Example for binary literals:
1. 8-bit byte value :
byte b = (byte)0b00010001;
2. 16-bit short value :
short s = (short)0b1000100010001000;
3. 32-bit int values :
int i1 = 0b001;
int i2 = 0B100;
int i3 = 0b10001000101010100001000100010001;
4. 64-bit long values :
long l = (long)0b100010001010101000010001000100011000100010101010101000010001L;
Example program: program given below will give you the understanding of binary literals.
package com.javatutorialscorner.java7.literals;

public class BinaryLiterals {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
byte b = (byte) 0b00010001;
short s = (short) 0b1000100010001000;
int i1 = 0b001;
int i2 = 0B100;
int i3 = 0b10100001010001011010000101000101;
long l = (long) 0b100010001010101000010001000100011000100010101010101000010001L;

System.out.println("Java 7 Binary literals");
System.out.println("byte b = " + b);
System.out.println("short s = " + s);
System.out.println("int i1 = " + i1);
System.out.println("int i2 = " + i2);
System.out.println("int i3 = " + i3);
System.out.println("long l = " + l);



Run the program see the following output
Java 7 Binary literals

byte b = 17

short s = -30584

int i1 = 1

int i2 = 4

int i3 = -1589272251

long l = 615481393887750673

Binary literals can make relationships among data more apparent  than they would be in hexadecimal or octal.For example, each successive number in the following array is rotated by one bit.

public static final int[] arr = {










In hexadecimal, the relationship among the numbers is not readily apparent:

public static final int[] arr = {
    0x31, 0x62, 0xC4, 0x89, 0x13, 0x26, 0x4C, 0x98

You can use binary integral constants in code that you can verify against a specifications document, such as a simulator for a hypothetical 8-bit microprocessor:

public State decoder(int instruction, State state) {

  if ((instruction & 0b11100000) == 0b00000000) {

    final int register = instruction & 0b00001111;

    switch (instruction & 0b11110000) {

      case 0b00000000: return state.nop();

      case 0b00010000: return state.copyAccumTo(register);

      case 0b00100000: return state.addToAccum(register);

      case 0b00110000: return state.subFromAccum(register);

      case 0b01000000: return state.multiplyAccumBy(register);

      case 0b01010000: return state.divideAccumBy(register);

      case 0b01100000: return state.setAccumFrom(register);

      case 0b01110000: return state.returnFromCall();

      default: throw new IllegalArgumentException();


  } else {

    final int address = instruction & 0b00011111;

    switch (instruction & 0b11100000) {

      case 0b00100000: return state.jumpTo(address);

      case 0b01000000: return state.jumpIfAccumZeroTo(address);

      case 0b01000000: return state.jumpIfAccumNonzeroTo(address);

      case 0b01100000: return state.setAccumFromMemory(address);

      case 0b10100000: return state.writeAccumToMemory(address);

      case 0b11000000: return state.callTo(address);

      default: throw new IllegalArgumentException();




You can use binary literals to make a bitmap more readable:

public static final short[] SHORT_ARR= {


















Note : some content in this tutorials is taken from Oracle official web site
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Java7 – Binary Literals Rating: 5 Reviewed By: Annamalai Thangaraj