Mastering Binary Numbers
Decimal Representation of Numbers. |
|
The most common numbering system we use is the decimal number system.
in this system, numbers are expressed as digits from 0 to 9. Each number
can be interpreted according to a system of place values, we proceed from
right to left. The digit in the extreme right represents the number of 1s,
the next digit the number of 10s, the next digit the number of 100s, and
so forth. For example, the number 2375 stands for:
| 5 | 1s | = | 1 * 5 | = | 5 |
| 7 | 10s | = | 10 * 7 | = | 70 |
| 3 | 100s | = | 100 * 3 | = | 300 |
| 2 | 1000s | = | 1000 * 2 | = | 2000 |
| | | | | | ------ |
| | | | | | 2375 |
If we carefully investigate this numbering representation, it can be seen
in another way:
| 2 * 103 | + 3 * 102 | + 7 * 101 | + 5 * 100 | = 2375 |
Note: that we have arranged the digits in their order of decreasing powers
of 10.
Binary Representation of Numbers. |
|
In the binary number system, numbers are represented by either 0 or 1 digits.
Just as the decimal number system is based on powers of 10, the binary number
system is based on powers of two. Let us take a binary number like: 10001111
starting from the right most digit which is corresponds to the number of 1s
the next digit to the number of 2s, the next digit to number of 4s and the next to 8s
and so forth. The binary number 10001111 can be written as:
1 * 27 | + 0 * 26 | + 0 * 25 | + 0 * 24 | + 1 * 23 | + 1 * 22 | + 1 * 21 | + 1 * 20 | = |
128 | + 0 | + 0 | + 0 | + 8 | + 4 | + 2 | + 1 | = 143 |
As a result the binary number 10001111 corresponds to the decimal number 143.
The binary system of numbers are extremely important in the operations of computers
as memory can be presented as on or off to correspond to binary digit of 0 or 1
this one binary digit is also called a bit. As we shall see, 8 bit and 16 bit
binary numbers play a special role in internal working of computers. The number
of 8 bit also called byte represents the number 0 through 255. Similarly the number
of 16 bit binary number (2 byte) represents the decimal numbers 0 through 65535.
Converting From Decimal to Binary. |
|
As we have seen, every binary number has a decimal equivalent,
however, the reverse is also true, every decimal number has a binary equivalent. For example, consider
the decimal number 41, in order to find the equivalent binary number we have to apply the following steps.
In each step we divides the number by 2 and write down the remainder.
| 41 | = | 20 * 2 | + 1 | Right most bit |
| 20 | = | 10 * 2 | + 0 | |
| 10 | = | 5 * 2 | + 0 | |
| 5 | = | 2 * 2 | + 1 | |
| 2 | = | 1 * 2 | + 0 | |
| 1 | = | 0 * 2 | + 1 | Left most bit |
And the resultant bit presentation of 41 decimal is 101001.
This process can be translated into a simple computer program:
Visual Basic Program
Function DecimalToBinary(ByVal Dec As Integer) As String
| 'Dec is the number to be converted |
| Dim Res as String |
| |
| Do While Dec > 0 |
| Res = Cstr(Dec MOD 2) & Res |
| Dec = Dec \ 2 |
| Loop |
| |
| return Res |
End Function
Hexadecimal Representation of Numbers. |
|
The hexadecimal number system is closely connected with the binary number system
and is much easier to work with in many applications, one hexadecimal digit is
corresponding to 4 binary digits, and so 8 bit binary numbers can be represented by
two hexadecimal digits. There are 16 possible hexadecimal digits:
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F |
The digits 0-9 have their usual decimal values and A, B, C, D, E, F have the
respective values
|
A = 10 |
B = 11 |
C = 12 |
D = 13 |
E = 14 |
F = 15 |
Let us take a hexadecimal number say A2BF and imply the translation to decimal
number system, as we done before we receive:
| 10 * 163 | + 2 * 162 | + 11 * 161 | + 15 * 160 | = |
| 40960 | + 512 | + 176 | + 15 | = 41663 |
Octal Representation of Numbers. |
|
The octal numbering system is based on 8 numeric digits, namely from 0 to 7, and as seen before it represents
powers of 8. For example the octal number 723 represent the decimal value of 467.
| 7 * 82 | + 2 * 81 | + 3 * 80 |
| 448 | + 16 | + 3 | = 467 |
In the following table we expressed the hexadecimal numbers by their corresponding
binary, octal and decimal values:
|
Hexadecimal | Binary | Octal | Decimal |
0 | 0000 | 0 | 0 |
1 | 0001 | 1 | 1 |
2 | 0010 | 2 | 2 |
3 | 0011 | 3 | 3 |
4 | 0100 | 4 | 4 |
5 | 0101 | 5 | 5 |
6 | 0110 | 6 | 6 |
7 | 0111 | 7 | 7 |
|
|
Hexadecimal | Binary | Octal | Decimal |
8 | 1000 | 10 | 8 |
9 | 1001 | 11 | 9 |
A | 1010 | 12 | 10 |
B | 1011 | 13 | 11 |
C | 1100 | 14 | 12 |
D | 1101 | 15 | 13 |
E | 1110 | 16 | 14 |
F | 1111 | 17 | 15 |
|
Note that each octal digit contains three binary digits. Therefore converting a binary to octal can be performed by breaking the
binary string up into consecutive groups of three starting from right, and then convert each group into an octal digit.
For example take the 16 bit binary number 1110011010100111 and convert it to octal number.
Binary number 1110011010100111
Divide into groups of three 1 110 011 010 100 111
Convert each group into octal 1 6 3 2 4 7
Final octal number 163247
Now converting from octal number to binary is the exact same thing except in reverse. For example take the octal number 27035 and convert it to binary.
Octal number 2 7 0 3 5
Convert each digit into binary 10 111 000 011 101
Final binary number 10111000011101
Converting Numbers from Decimal to Any Base. |
|
Now we can generalize the conversion process to include conversions from decimal to any base.
For example, convert the decimal number 6478 to base 24, The conversion process includes the following steps:
Step 1: Find the remainder of division by the new base (this is the Mod operation).
| 6478 Mod 24 = 22 | The corresponding character to 22 is M. |
Step 2: Perform integer division of the original decimal value by the new base, the result is.
Step 3: Return to step 1 and Perform Mod operation on the division result, that gives.
|
269 Mod 24 = 5 |
The corresponding character to 5 is 5.
This value will be added to the left of the previous character M to form 5M.
|
Step 4: Return to step 2 and Perform integer division on the previous Mod result.
Step 5: Return to step 1 and Perform Mod operation of the last integer division.
|
11 Mod 24 = 11 |
The corresponding character to 11 is B and it is
added to the left to yield the final conversion value of B5M
|
Visual Basic Program to Convert a Decimal Number to Any Other Base
Converting Numbers from Any Base to Decimal. |
|
We can represent the conversion process mathematically as follows:
As seen in the above equation, the equivalent decimal number is the sum of the characters value in decimal, starting from
right, multiplied by the base value. If for example we take the number 6MA in base 24 the conversion to decimal
will be:
Visual Basic Program to Convert a Number in Any Base to Decimal Number
Binary Right and Left Shift. |
|
In some applications it is often required to move all the bits of a certain binary
number to the left or to the right. Such operations are called a left shift or a
right shift. For example, consider this 8-bit (byte) binary number: 10110111,
which is equivalent to 183 in decimal representation. If we apply a left shift, we
obtain the binary number 01101110 = 110 decimal notes that a zero was added at the
right most digit and the original left most bit has been pushed off the end.
Similarly, a right shift applied to the binary number 10110111 yields 01011011 = 91 decimal
then a second right shift can be applied to yield 00101101 = 45.
The shift operation can be summarized as in the next tables.
A computational left shift can be done by multiplying the decimal value by 2, this can lead to the
left most bit to be pushed to the 9th place. In order to eliminate the 9th bit, we perform the MOD
operation (division remainder) on the decimal number by the number 256, this operation will return only the 8
right most bits.
| Left shift | = | 2 * Decimal | MOD | 256 |
A right shift on the other hand can be done by integer division of the decimal value by 2, that is
In the above examples we assumed that right and left shifts were operated on 8 bit (byte)
binary number, but shift operation can be performed also on 16 bit (2 byte or a word)
or 32 bit or on any number of bits that we wish, in this case the same rules of shifting
applies, for example the 16-bit binary number 11100110 10111011 can be left shifted to obtain
11001101 01110110.
| 11100110 10111011 = 59067 Decimal |
| Left shift = 59067 * 2 = 118134 Decimal |
| 118134 MOD 65536 = 52598 Decimal = 11001101 01110110 |
The purpose of the division by MOD 65536 is to eliminate the bit pushed to the 17th place by the shift operation.
The next table summarizes the MOD operator values to obtain N number of bits:
|
Bits | Decimal value |
4 | 16 |
8 | 256 |
16 | 65536 |
24 | 16777216 |
32 | 4294967296 |
N | 2 ^ N |
|
Logical Operations on Bits.
If A and B are two binary numbers with similar length, then we can compare A and B bit by bit
for a given bit position and perform a logical operation like AND, OR, XOR and NOT. The use of
the logical operations is extremely important as it allows us to control switch states, by
setting a certain bit on or off.
Logical AND Operation. |
|
If two corresponding bits of A and B have a one, then the result is a one, if either A or B has a zero, the result
is a zero. For example, consider the following 16-bit binary numbers:
| A | = | 1011 1011 1000 0011 | = 48003 decimal |
| B | = | 1110 1010 1111 1010 | = 60154 decimal |
| A AND B | = | 1010 1010 1000 0010 | = 43650 decimal |
Switching control
Suppose we have an 8-bit number like 11110011, this array of bits can simulate 8 switches that each of them
can be in on or off position. Let’s think what happens if we need to turn bit 6 (from right or switch number 6) to off.
This process is done by the AND operation, in order to find the second number needed for the AND operation, we will choose the
binary number 11011111. All bits are 1 except the one we want to turn off.
| A | = | 11110011 | = 243 decimal |
| B | = | 11011111 | = 223 decimal |
| A AND B | = | 11010011 | = 211 decimal |
Note that all the bits remained untouched, except bit number 6 that has been turned off. The same result would be
obtained by the operation on the equivalent decimal numbers 243 AND 223 = 211
By applying the AND operation we can only turn switches off in order to turn a bit on we need to apply the OR operation as described in the next paragraph.
We can turn off multiple switches in a single operation, this will be done by choosing zeroes in the second number in the places we want to turn them off,
like 10001101 in this case we will turn off 4 bits (all the 0 values).
Logical OR Operation. |
|
The OR operation performed on two identical length binary numbers is obtained by comparing A and B
bit by bit. For a given bit position, if either A or B have a one, then the corresponding bit
of A OR B is one. If both A and B have a zero, the corresponding bit of A OR B is a zero.
For example, consider the two 8-bit binary numbers
| A | = | 01010011 | = 83 decimal |
| B | = | 11010101 | = 213 decimal |
| A OR B | = | 11010111 | = 215 decimal |
Switching control
By performing the OR operation we can turn on a single switch, this is done as follows, Lets take the switch array 11010111 and try to
turn on switch number 6 (from right). The second binary number is chosen to be 00100000, all bits are zero except the one we want to turn on.
| A | = | 11010111 | = 215 decimal |
| B | = | 00100000 | = 32 decimal |
| A OR B | = | 11110111 | = 247 decimal |
Note that switch number 6 is now turned on and all other switches remained untouched.
Logical XOR Operation. |
|
The operation A XOR B is called the exclusive OR, it is performed on two identical length binary numbers by comparing A and
B bit by bit. For a given bit position, if A and B have different bits values, then the corresponding bit of A and B
is a one. If A and B have the same bits value, the corresponding bit A XOR B is a zero. For example, consider the 8-bit
binary number A = 11001001 and B = 10001101 then,
| A | = | 11001001 | = 201 decimal |
| B | = | 10001101 | = 141 decimal |
| A XOR B | = | 01000100 | = 68 decimal |
If A and B are integers given in decimal form, we may also apply the operation XOR the same way like
201 XOR 141 = 68.
An interesting use of The XOR operation is in the field of encoding text expressions.
Logical NOT Operation. |
|
The NOT operation reverses all the bits of a given number. For example, consider the binary number 00111001. We may compute this number by changing
every 0 to 1 and every 1 to 0, see the next lines.
| A | = | 00111001 | = 57 decimal |
| NOT A | = | 11000110 | = 198 decimal |
The NOT operation can be performed on decimal numbers as well. For example, NOT 57 = 198.
It is strictly important to define previously the length of the binary number, as different lengths has different NOT values.
The above calculations were based on 8-bit length, if the same values were calculated on 16 bit length, the results would be as follows,
| A | = | 00000000 00111001 | = 57 decimal |
| NOT A | = | 11111111 11000110 | = 65478 decimal |
Logical operations on A and B
A |
B |
AND |
OR |
XOR |
NOR |
XNOR |
NAND |
NOT A |
NOT B |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
Encoding text. |
|
One interesting use of the XOR operation is for encoding purposes, this encoding is based on the fact that by performing the XOR
operation twice on a certain character, we receive back the original character. The value for the encoding can be selected to be
any value as our imagination goes.
For example examine the letter "s", this letter has the equivalent Ascii value of 115 or in binary presentation A = 01110011.
For the encoding code we choose the value 66 or in binary presentation B = 01000010, now we apply the XOR operation to receive.
| A | = | 01110011 | = 115 decimal |
| B | = | 01000010 | = 66 decimal |
| C = A XOR B | = | 00110001 | = 49 decimal |
So, we encoded the letter "s" by the Ascii code of 49 which is the digit "1". Now applying the encoding value once again to restore the original
letter yields.
| C | = | 00110001 | = 49 decimal |
| B | = | 01000010 | = 66 decimal |
| A = C XOR B | = | 01110011 | = 115 decimal |
As we expected the original letter has been restored. The only rule we have to remember is to apply the same encoding value twice. By applying this technique on each
letter, we can encode the entire document.
The above example uses the same encoding value to all the characters. To further improve the encoding process, we can apply a trigonometric function or a varying encoding value.
For example, we can define an array of arbitrary values like 3, 44, 77, 295, 3393, 858, 44, 338 and so on, then apply this encoding values, each of them on a progressing character, the
only thing to remember is to apply the same encoding values to restore the original letter.