"Can you do Addition?" the White Queen asked.
"What's one and one and one and one and one and one and one and one and one and one?"
"I don't know," said Alice. "I lost count." — Lewis Carroll, Through the Looking Glass
The number system we are all familiar with is the decimal number system, or base 10 number system. Computers, on the other hand, use the binary number system, which is base 2. Other number systems, notably hexadecimal (base 16) and, to a lesser degree, octal (base 8) are also used in the study and programming of computers, but only because they provide a more compact notation for representing binary values. Internally the computer only knows the binary system. As will be shown, hexadecimal is shorthand for binary.
In order to understand "alien" number bases it is helpful to analyze the decimal system. Try to make a connection between "10" and the various properties of our base 10 system. Then, when discussing other number bases, make the same connection between the number base in question: base 2, base 16, or even, if you want, base 7 (or base 13, or base ...) and the properties of that number base. The important point here is that, regardless of the number system, the relationships between the number base and its properties follow the same rules. Always.
Consider the decimal number 375 (assume base 10 unless another number base is specifically stated). We know that, starting from the right side (called the least significant digit) and working to the left, there is a "ones" place, a "tens" place, and a "hundreds" place. Remember, this is base 10; note that each column, or place value, is 10 times the value of the column or place to its right. The value of the number is calculated by multiplying the value of each column times the number in that column, then adding all the products. In the example we have 5 ones + 7 tens + 3 hundreds:
hundreds tens ones 5
3 7 5 70
+300
3*100 7*10 5*1 ----
300 70 5 375
Continuing with base 10, note that we have 10 digits. (Do you think it is just a coincidence that the appendages on our hands are also called "digits"?). The digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Note also that the name of our number system, base 10, cannot be written with a single base 10 digit.
One more rule and you have all you need to calculate the value (in base 10) of a number written in any other base. That rule is: all number systems have a "ones" column.
So, given the binary number (base 2) 1101, we know this:
eights fours twos ones 1
1 1 0 1 0
4
1*8 1*4 0*2 1*1 +8
8 4 0 1 ---
13
Sometimes it is convenient to think of each column, or place, being a power of the base, always starting with the base to the 0 power. Since any number or value (except 0) to the 0 power is 1, we see again that all number bases have a "ones" place.
Place values (e.g. column values) for various number bases:
| Base b | Value = bn | . . . | Value = b4 | Value = b3 | Value = b2 | Value = b1 | Value = b0 |
|---|---|---|---|---|---|---|---|
| 10 | 10n | . . . | 10000 | 1000 | 100 | 10 | 1 |
| 2 | 2n | . . . | 16 | 8 | 4 | 2 | 1 |
| 16 | 16n | . . . | 65536 | 4096 | 256 | 16 | 1 |
| 7 | 7n | . . . | 2401 | 343 | 49 | 7 | 1 |
| b | bn | . . . | b4 | b3 | b2 | b1 | b0 |
To summarize:
[ TOP ]
[ TOP ]
| 64's | 32's | 16's | 8's | 4's | 2's | 1's |
| 1 | ? | ? | ? | ? | ? | ? |
|---|
| 64's | 32's | 16's | 8's | 4's | 2's | 1's |
| 1 | 1 | ? | ? | ? | ? | ? |
|---|
| 64's | 32's | 16's | 8's | 4's | 2's | 1's |
| 1 | 1 | 1 | ? | ? | ? | ? |
|---|
| 64's | 32's | 16's | 8's | 4's | 2's | 1's |
| 1 | 1 | 1 | 0 | ? | ? | ? |
|---|
| 64's | 32's | 16's | 8's | 4's | 2's | 1's |
| 1 | 1 | 1 | 0 | 0 | ? | ? |
|---|
| 64's | 32's | 16's | 8's | 4's | 2's | 1's |
| 1 | 1 | 1 | 0 | 0 | 0 | ? |
|---|
| 64's | 32's | 16's | 8's | 4's | 2's | 1's |
| 1 | 1 | 1 | 0 | 0 | 0 | 1 |
|---|
The conversion method shown above is rather long and involved, but it is descriptive and intuitive. A shorter and quicker method for quick conversions is shown below at the risk of not clearly showing the underlying process:
0 remainder = 1 Repeat dividing decimal number by
2)1 remainder = 1 2 until answer = 0, keeping a list
2)3 remainder = 1 of the remainder from each division.
2)7 remainder = 0
2)14 remainder = 0 Reading the list of remainders from
2)28 remainder = 0 top to bottom will be the binary number:
2)56 remainder = 1
2)113 113 decimal = 1110001 binary
[ TOP ]
| 256's | 16's | 1's |
| C | ? | ? |
|---|
| 256's | 16's | 1's |
| C | 1 | ? |
|---|
| 256's | 16's | 1's |
| C | 1 | 2 |
|---|
[ TOP ]
|
|
2 combinations (states) |
|
2 * 2 = 4 combinations (states) |
|
2 * 2 *2 = 8 combinations (states) |
Do you see the pattern?
| 1 bit: | 2 | ( 21 ) | = 2 states |
| 2 bits: | 2 * 2 | ( 22 ) | = 4 states |
| 3 bits: | 2 * 2 * 2 | ( 23 ) | = 8 states |
| 4 bits: | 2 * 2 * 2 * 2 | ( 24 ) | = 16 states |
| | |||
| 8 bits: | 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 | ( 28 ) | = 256 states |
| | |||
| n bits: | 2 * 2 * 2 . . . | ( 2n ) | = 2n states |
[ TOP ]
| 11110001011010base 2 | a 14-digit binary number | |||
| 0011 | 1100 | 0101 | 1010 | 4-digit groups plus 2 leading 0's |
| 3base 10 | 12base 10 | 5base 10 | 10base 10 | decimal value of each 4-bit group |
| 3base 16 | Cbase 16 | 5base 16 | Abase 16 | hex equivalent |
| 3C5Abase 16 | = 11110001011010 binary | |||
[ TOP ]
Paid: $10.00
Due: $ 8.45
Is stored in the computer as:
Binary Data (hex) ASCII text
----------------------------------------------- ----------------
50 61 69 64 3A 09 24 31-30 2E 30 30 0D 0A 44 75 Paid:.$10.00..Du
65 3A 09 24 20 38 2E 34-35 0D 0A e:.$ 8.45..
Chr Ctrl Dec Hex Chr Dec Hex Chr Dec Hex Chr Dec Hex
NUL ^@ 0 0 SP 32 20 @ 64 40 ` 96 60
SOH ^A 1 1 ! 33 21 A 65 41 a 97 61
STX ^B 2 2 " 34 22 B 66 42 b 98 62
ETX ^C 3 3 # 35 23 C 67 43 c 99 63
EOT ^D 4 4 $ 36 24 D 68 44 d 100 64
ENQ ^E 5 5 % 37 25 E 69 45 e 101 65
ACK ^F 6 6 & 38 26 F 70 46 f 102 66
BEL ^G 7 7 ' 39 27 G 71 47 g 103 67
BS ^H 8 8 ( 40 28 H 72 48 h 104 68
HT ^I 9 9 ) 41 29 I 73 49 i 105 69
LF ^J 10 A * 42 2A J 74 4A j 106 6A
VT ^K 11 B + 43 2B K 75 4B k 107 6B
FF ^L 12 C , 44 2C L 76 4C l 108 6C
CR ^M 13 D - 45 2D M 77 4D m 109 6D
SO ^N 14 E . 46 2E N 78 4E n 110 6E
SI ^O 15 F / 47 2F O 79 4F o 111 6F
DLE ^P 16 10 0 48 30 P 80 50 p 112 70
DC1 ^Q 17 11 1 49 31 Q 81 51 q 113 71
DC2 ^R 18 12 2 50 32 R 82 52 r 114 72
DC3 ^S 19 13 3 51 33 S 83 53 s 115 73
DC4 ^T 20 14 4 52 34 T 84 54 t 116 74
NAK ^U 21 15 5 53 35 U 85 55 u 117 75
SYN ^V 22 16 6 54 36 V 86 56 v 118 76
ETB ^W 23 17 7 55 37 W 87 57 w 119 77
CAN ^X 24 18 8 56 38 X 88 58 x 120 78
EM ^Y 25 19 9 57 39 Y 89 59 y 121 79
SUB ^Z 26 1A : 58 3A Z 90 5A z 122 7A
ESC ^[ 27 1B ; 59 3B [ 91 5B { 123 7B
FS ^\ 28 1C < 60 3C \ 92 5C | 124 7C
GS ^] 29 1D = 61 3D ] 93 5D } 125 7D
RS ^^ 30 1E > 62 3E ^ 94 5E ~ 126 7E
US ^_ 31 1F ? 63 3F _ 95 5F DEL 127 7F
[ TOP ]
| Assume the letter "a" is represented as a black and white bit- map image in a rectangular grid 7 pixels wide by 9 pixels high: |
|
Replace each white pixel with a "clear bit" (0) and each black pixel with a "set bit" (1): |
0000000 0011100 0100010 0000010 0011110 0100010 0100110 0011010 0000000 |
0000000 0011100 0100010 0000010 0011110 0100010 0100110 0011010 0000000
If the following stream of bits represent a rectangular black and white image
as a bit map, what familiar object is it?
11100100101001010010111001001010001
The above examples were black and white, therefore only 1 bit was needed to
represent each pixel since 1 bit can represent 2 states: black or white.
[ TOP ]
041C ; ADD AL,1C
04 hex is the opcode; it says "add the immediate value" specified
in the operand to whatever is already in the AL register and
store the result in the AL register.
1C hex is the operand (the decimal number 28). The AL register is
the lower half of the accumulator.
Another example:
BE8000 ; MOV SI,80
8B04 ; MOV AX,[SI] DS:0080=0D00
BE hex is the 1st opcode; it says "load the immediate value"
specified in the operand (80 Hex) in the SI register.
8B hex is the next opcode; it says "load whatever is stored at the address designated in the register represented by the operand" (04 Hex, the SI register in this example) into the AX register.
[ TOP ]
| Memory | ||||
|---|---|---|---|---|
| Low | > > | > > | High | |
| Binary | 00110101 | 00100001 | 01010001 | 01010000 |
| Hexadecimal | 35base 16 | 21base 16 | 51base 16 | 50base 16 |
| 8-bit integers | 53D ( 35H ) | 33D ( 21H ) | 81D ( 51H ) | 80D ( 50H ) |
| 16-bit integers (Intel) | 8501D ( 2135H ) | 20561D ( 5051H ) | ||
| 16-bit integers (Motorola) | 13601D ( 3521H ) | 20816D ( 5150H ) | ||
| 32-bit integer (Intel) | 1347494197D ( 50512135H ) | |||
| 32-bit integer (Motorola) | 891375952D ( 35215150H ) | |||
Intel processors are an example of "little endian" byte order. Numbers that are bigger than 1 byte are stored in memory from low address to high address with the least significant bytes at the low addresses. For example, given the hex number 1234 (4660 decimal), 34 is the least significant byte (also called the "low order" byte) and 12 is the most significant (high order) byte. Little endian byte order places 34 in memory first, followed by 12. Big endian byte order places the high order bytes in memory before low order bytes for multi-byte values.
[ TOP ]
Consider the place values table presented earlier for base 2. If we continue the pattern of decreasing powers of 2 moving farther to the right then it follows that the next power is -1, -2, etc.
| Base b | Value = bn | . . . | Value = b2 | Value = b1 | Value = b0 | . | Value = b-1 | Value = b-2 | Value = b-3 |
|---|---|---|---|---|---|---|---|---|---|
| 2 | 2n | . . . | 4 | 2 | 1 | . | 1/2 | 1/4 | 1/8 |
We put the "decimal point" (which should probably be called the
binary point) between the "ones" column and the "one-halfs" column.
Converting the binary number 11.101 to decimal is done
thus:
(1 * 2) + (1 * 1) + (1 * 0.5) + (0 * 0.25) + (1 * 0.125)
2 + 1 + 0.5 + 0.0 + 0.125 =3.625
=35/8
A "shortcut" is to look at the binary digits right of the decimal point:
101. The least significant digit is the "one-eights" digit; and the
binary number right of the "decimal point" converted to decimal is 5.
The fractional part of the number is going to be expressed in eights, and there
are 5 of them: 5/8.
Converting 4.6 decimal is just like any other decimal to binary
conversion:
4 has 1 four, 0 twos, and 0 ones, so the whole number part is
100 binary.
.6 has 1 one-half, with .1 remaining; .1 has 0 one-fourths (.25), 0 one-
eights (.125), but has 1 one-sixteenth (.0625) with 0.0375 remaining. At
this point (4 binary decimal places) 4.6 decimal is
100.1001 binary. We would then continue this process for as many
binary decimal places of precision we needed. Try it; how many decimal
places do you think you have to work the conversion until it finally end
up with no remainder?
Another shortcut, although not quite as short as the previous one:
How many binary "decimal places" do you want?
Say you want 8 binary "decimal places"; you will have a
1/2 digit, a 1/4 digit,
1/8, . . . , and finally a 1/256
digit.
The fractional part is going to be "two-hundred-fifty-sixths", so ask
yourself "6/10 is how many two-hundred-fifty-
sixths?"
Do a simple ratio:
6/10 = x/256
10x = 1536
x = 154 (rounded up)
154 decimal = 10011010
Therefore 4.6 decimal = 100.10011010 binary, rounded to 8 binary "decimal places."
Be careful with fractions less than 0.5. For example, 0.1 has no 1/2, 1/4, or 1/8. Using the method shown above 1/10 = 26/256 and 26 decimal = 11010 binary. Remember, however, that we want a binary fraction in 1/256's, which is 8 binary fractional digits. We must "lead" 11010 with zeros to fill all 8 binary digits right of the "decimal point:" .00011010
[ TOP ]