Binary Literals Tutorial

Binary literals are whole numbers of the base 2 numbering system. They are a sub-grouping of the general category of integer literals. Binary literals are whole numbers that can be assigned to variables of the following data types: byte, char, short, int, and long. Binary literals begin with a 0B or 0b prefix.

  • int a = 0B00000011;
  • int b = 0b00001111;
This tutorial will teach you how the binary numbering system works and how to assign binary literals to variables.



Review of the Decimal System

Everybody knows the decimal system, you can count from 1-100 and then keep on going forever if you wanted to. Try to remember way back to elementary school math, we learned that numbers in the decimal system are organized into columns. Because you have been using the decimal system so long, you might not even realize that the columns are read from right to left. The rightmost column is the ones, next is the tens, next is the hundreds, next is the thousands, and so on an so forth.

        Thousands | Hundreds | Tens | Ones  
7,491      7           4        9      1     
832                    8        3      2   
41                              4      1   
9                                      9   

The Caret Symbol ^ and Exponents

Exponents are shorthand for multiplication of the same number over and over and over again. The number of times a number is multiplied by itself is represented following the caret symbol ^.
Here is how it works: (number) (caret ^ also stated as "to the power of") (number of times to repeat multiplication )
3^3 = 3 x 3 x 3 = 27
4^2 = 4 x 4 = 16
5^3 = 5 x 5 x 5 = 125
2^5 = 2 x 2 x 2 x 2 x 2 = 32
Pretty basic, right?

Bits and the Byte

Your hard drive, thumb drive, SD card are all examples of disk memory storage devices. Typically, each one has a memory capacity listed in megabytes, gigabytes, or terabytes. Let's imagine that we have an empty thumb drive. If we could "peel" it open and look the memory, we would see millions of 0's all lined up.
A bit is a single memory location that contains either a 1 or 0; there are 8 bits in a byte.
What would a single empty byte look like? 00000000

The Binary Numbering System

Just like the decimal system, the binary system is read from right to left. The rightmost column is 2^0, next is 2^1, next is 2^2, next is 2^3, and so on an so forth. You may recall that any number raised to the power of zero is 1. If the column has a 1 in it, you simply add the max value of that column to any other columns that have a 1 in them. Here are decimal numbers 0-15 in a binary chart:

        2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0  
        64  | 32  | 16  | 8   | 4   | 2   | 1    
        ---------------------------------------------   
0        0     0     0     0     0     0     0  
1        0     0     0     0     0     0     1  
2        0     0     0     0     0     1     0  
3        0     0     0     0     0     1     1   1+2=3
4        0     0     0     0     1     0     0  
5        0     0     0     0     1     0     1   1+4=5
6        0     0     0     0     1     1     0   2+4=6
7        0     0     0     0     1     1     1   1+2+4=7
8        0     0     0     1     0     0     0  
9        0     0     0     1     0     0     1   1+8=9
10       0     0     0     1     0     1     0   2+8=10
11       0     0     0     1     0     1     1   1+2+8=11
12       0     0     0     1     1     0     0   4+8=12
13       0     0     0     1     1     0     1   1+4+8=13
14       0     0     0     1     1     1     0   2+4+8=14
15       0     0     0     1     1     1     1   1+2+4+8=15

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md BinaryLiterals
C:\Java>cd BinaryLiterals
C:\Java\BinaryLiterals>Notepad BinaryLiterals.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class BinaryLiterals {
    public static void main(String args[]) {
        byte a = 0B00000001; // the leading zeros are optional
        byte b = 0b11; //the 0b prefix can be either uppercase B or lowercase b
        short c = 0B0100001;
        short d = 0b1111111;
        int e = 0b01_000011111010101_0101; // the optional underscore can be used with binary literals as well.
        int f = 0b00000000;
        char upperA = 0B1000001;
        char upperB = 0b1000010;
        long g = 0b1111111_11111111_11111111_11111111;
        long h = 0b11111111_11111111_11111111_11111111_11111111_10101010L;

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);
        System.out.println(upperA);
        System.out.println(upperB);
        System.out.println(g);
        System.out.println(h);
    }
}

Now switch back to the command prompt (CMD) and type in javac BinaryLiterals.java and press Enter.
Now type in java BinaryLiterals and press Enter.


C:\Java\BinaryLiterals>javac BinaryLiterals.java
C:\Java\BinaryLiterals>java BinaryLiterals
1
3
33
127
556373
0
A
B
2147483647
281474976710570


Final thoughts

I purposely ignored how to represent negative binary numbers in this tutorial. In Java, negative numbers are represented in binary using a mathematical operation called two's compliment. The concept of two's compliment definitely deserves its own separate tutorial.


Tutorials