Java 7 Binary Literals

In this tutorials we are going to see the integral types (byte, short, int, and long ) in java 7. This tutorials is created by referring Oracle official web site.Some content is taken from there.

In Java 7, 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.ehowtonow.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);



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

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java 7 Try statement with multi catch
  2. Java 7 Underscores in Numeric Literals
  3. Java 7 Binary Literals
  4. Java 7 Try statement with resources
  5. Java 7 Switch statement on String
  6. Java 7 Re-throw of an Exception
  7. Java 7 new features

Be the first to comment

Leave a Reply