Binary numbers

Data representation

What you should know:

In the previous lesson, we were talking about the behavior of sequences of several bits. We showed that we were able to double the number of combinations that could be attained, just adding a bit to the left and repeating the same sequence twice, where the leftmost bit changes from 0 to 1.

So, if we start with just one bit:

One bit
0Always start from 0
1And then 1
From one to two bits
00Repeat the previous pattern adding a 0 to the left
01
10Change the leftmost bit to 1 while the rightmost bit repeats the pattern of the two previous rows.
11

Usually the zeroes located in the leftmost part of the combination can be omitted, so that 01 can be considered equivalent to 1 and 00 to just 0.

Using the same algorithm we can easily write the combinations of any number of bits.

Combinations of 4 bits
00
11
102
113
1004
1015
1106
1117
10008
10019
101010
101111
110012
110113
111014
111115
First two combinations adding the fifth bit
1000016
1000117

Writing the combinations in the right order we can associate each sequence of bits with an integer number. Some of the combinations in the previous table have a specific property: the leftmost bit equals 1, while all the other bits are 0. These sequences are associated with a power of two, starting from 2^0=1.

Sequences with just a 1 in the leftmost position
112^0=1
1022^1=2
10042^2=4
100082^3=8
10000162^4=16

If more than one bit equals one, the value of the associated integer can be calculated by adding the value of every bit in the sequence, that does not equal 0. For example, the value of 1100, can be calculated, in binary and in decimal notation, as follows:

1000+100=1100 \Rightarrow 8+4=12

In the same way, the value of 1110 can be calculated as follows:

1000+100+10=1110 \Rightarrow 8+4+2=14

The value associated to any bit in the sequence doubles of halves if it’s moved one position to the left or to the right. For this reason dividing or multiplying by two in binary is as easy as removing or one bit from the right or adding a 0 in the same position.

For instance if we want to multiply 1100 by two, we can write:

1100 \cdot 10 = 1000\cdot10+100\cdot10= 10000+1000=11000

In decimal numbers the same could be written as:

12\cdot2 = 8\cdot2+4\cdot2=16+8=24

This also means that a number ending with a zero on the right is even, while a number ending with a 1 is odd.