"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:
(5 * 1) + (7 * 10) + (3 * 100)
5 + 70 + 300 = 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:
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.
- Any number or value raised to the 0 power = 1
20 = 1,
100 = 1,
160 = 1,
70 = 1,
n0 = 1,
Toyota0 = 1
- Any number or value raised to the 1st power = itself
21 = 2,
101 = 10,
161 = 16,
71 = 7,
n1 = n
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:
- All number systems have a "ones" column
- Starting from the right:
- The next column to the left is always 1 times the
base
- The next column after that is always the square of
the base (base * base,
base2
- The next column after that is always the base to
the third power base3
- . . .
- Any column is always the base times the value of
the column to its immediate right
- The number that represents the base of the number system
cannot be written in a single digit of that number system:
- Base "ten" = 10base 10
- Base "two" = 10base 2
- Base "sixteen" = 10base 16
- Base "seven" = 10base 7
- Base "n" = 10base n
[ TOP ]
- 327base 10
is 3 hundreds + 2 tens + 7 ones
= 300 + 20 + 7
= 327
- 11010base 2
is 1 sixteen + 1 eight + 0 fours + 1 two + 0 ones
= 16 + 8 + 2
= 26base 10
- Hexadecimal uses A - F for the digits representing 10 - 15
- 3C5Abase 16
is 3 4096's + 12 256's + 5 sixteens + 10 ones
= 12288 + 3072 + 80 + 10
= 15450base 10
- 4632base 7
is 4 343's + 6 forty-nines + 3 sevens + 2 ones
= 1372 + 294 + 21 + 2
= 1689base 10
- Given any base n, there are n
digits to represent the number system:
- Base 10 = 10 digits: 0, 1, 2, . . . , 8, 9
- Base 2 = 2 digits: 0, 1
- Base 16 = 16 digits: 0, 1, 2, . . . , E, F
- Base 7 = 7 digits: 0, 1, 2, . . . , 5, 6
- Base n = n digits: 0, 1, 2, . . . , n-1
[ TOP ]
- Convert 113base 10 to binary:
-
Q: What is the biggest power of 2 that will go into 113?
A: 64, with 49 left over.
-
My binary number will have 7 digits: a 1's place, a 2's place,
a 4's place, an 8's place, a 16's place, a 32's place, and
finally a 64's place.
I have one 64; my number so far is 1 _ _ _ _ _ _
| 64's | 32's |
16's | 8's |
4's | 2's |
1's |
| 1 | ? | ? | ? |
? | ? | ? |
- 113 - 64 = 49 remaining
The next place value below 64 is 32.
Q: Will 32 go into 49?
A: Yes; I have one 32; my number so far is 1 1 _ _ _ _ _
| 64's | 32's |
16's | 8's |
4's | 2's |
1's |
| 1 | 1 | ? | ? |
? | ? | ? |
- 49 - 32 = 17 remaining
The next place value below 32 is 16.
Q: Will 16 go into 17?
A: Yes; I have one 16; my number so far is 1 1 1 _ _ _ _
| 64's | 32's |
16's | 8's |
4's | 2's |
1's |
| 1 | 1 | 1 | ? |
? | ? | ? |
- 17 - 16 = 1 remaining
The next place value below 16 is 8.
Q: Will 8 go into 1?
A: No; I have no 8's; my number so far is 1 1 1 0 _ _ _
| 64's | 32's |
16's | 8's |
4's | 2's |
1's |
| 1 | 1 | 1 | 0 |
? | ? | ? |
- Q: Will 4 go into 1?
The next place value below 8 is 4.
A: No; I have no 4's; my number so far is 1 1 1 0 0 _ _
| 64's | 32's |
16's | 8's |
4's | 2's |
1's |
| 1 | 1 | 1 | 0 |
0 | ? | ? |
- Q: Will 2 go into 1?
The next place value below 4 is 2.
A: No; I have no 2's; my number so far is 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 | ? |
- Q: Will 1 go into 1?
The next place value below 2 is 1.
A: Yes; I have one 1; my number is 1 1 1 0 0 0 1
| 64's | 32's |
16's | 8's |
4's | 2's |
1's |
| 1 | 1 | 1 | 0 |
0 | 0 | 1 |
- 113base 10 =
1110001base 2
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 ]
- Convert 3090base 10 to hexadecimal:
- It might help to list the powers of 16 until we exceed our
number:
1, 16, 256, 4096, . . .
- Note that the hexadecimal number system has 16 digits; we use
the characters A, B, . . . F to represent the decimal
values 10 through 15.
- Q: What is the biggest power of 16 that will
go into 3090 and how many times?
A: 256 will go into 3090 12 times with 18 left over.
- My hex number will have 3 digits: a 1's place, a 16's
place, and a 256's place.
I have 12 256's (12 decimal = C hex);
thus my number so far is C _ _
- 3090 - (12 * 256) = 3090 - 3072 = 18 remaining.
Q: Will 16 go into 18? How many times?
A: Yes, 1 time with 2 left over.
I have one 16; my number so far is C 1 _
- 18 - 16 = 2 remaining
Q: Will 1 go into 2?
A: Yes; I have 2 ones; my number is C 1 2
- 3090base 10 =
C12base 16
[ TOP ]
-
The smallest unit of data in a computer is a bit (from:
"binary digit").
-
The next unit of data is a byte, which is a group of 8 bits
on all modern computer systems. The value of a byte, or stream of
bytes, is usually written in hexadecimal (base 16) rather than
binary because hexadecimal is a convenient shorthand for binary (see
the next section).
-
Groups of bytes are called words. The size of a word varies
depending on the computer architecture, but will be a power of 2 and
is determined by the biggest chunk of bits that the processor can
work with at a time. (Technically, the size of the accumulator
defines the word size for a processor) For example an "8-bit"
computer would have a word size of 8 bits, or 1 byte. Early IBM PC's
used the Intel 8080 processor (8-bit; 1-byte word), and the 8086
processor (16-bit; 2-byte word).
-
Two recurring questions when working with computers are:
-
Given the number n, how many bits do we need to store the
number?
Put another way, given m items (colors, ASCII characters,
flavors of ice cream, etc.), how many bits are required to
count, or represent all the possibilities?
-
Given x number of bits (or bytes), how many different
things can we count or represent?
How many shades of gray can be represented in 4 bits?
How many ASCII characters can be represented in one byte (8 bits)?
How many indices in a table can be represented in 2 bytes (16-bits)?
-
One bit can represent 2 things (2 states in computer-speek):
|
|
2 combinations (states) |
-
If we add one more bit (total of 2 bits), then we double the
possibilities: the 2 states of the first bit are combined with
each state of the second bit:
|
|
2 * 2 = 4 combinations (states) |
-
If we add yet one more bit (total of 3 bits now) we double the
number of possible combinations yet again (to 8).
|
|
2 * 2 *2 = 8 combinations (states) |
Do you see the pattern?
-
Starting with 2 combinations for 1 bit, each additional bit
doubles the number of combinations (states):
| 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 |
-
The number of states that can be represented (or number of items
that can be counted) by n binary digits is
2n
-
Zero is a valid state.
-
Zero is a valid counting number and is commonly the first address,
index, etc. when computers count.
-
The "first" item will be 0
-
The "last" item will be
( 2n - 1 )
-
4 binary digits (bits) can represent 16 different states or count up to
16 different things
- 16 shades of gray (0, 1, 2, . . . 15)
- 16 processor instructions (0000, 0001, 0010, ... 1111)
- 8 bits = 256 ASCII characters (0 - 255)
- 16 bits = 65,536 memory addresses (0 - 65,535)
- 24 bits = 16,777,216 colors (0 - 16,777,215)
- 32 bits = 4,294,967,296 IP addresses (0 - 4,294,967,295)
- 128 bits = a lot of something
( 0 - (2128 - 1) )
-
The number of bits needed to count n items or
represent n states is the smallest power of 2 that is equal
to or greater than n.
-
How many bits needed to represent 8 shades of gray?
-
21 = 2: not enough bits!
-
22 = 4: not enough bits!
-
23 = 8: bingo!
We have enough bits (3) to represent 8 shades of gray.
-
31 flavors of ice cream:
25 = 32
therefore 5 bits required to represent 31 flavors of ice cream
-
1000 Customers:
210 = 1024
therefore 10 bits required to represent 1000 Customer ID numbers
Note, if counting bytes, 2 bytes are required
[ TOP ]
-
Hexadecimal (base 16) is shorthand for binary.
Internally the
computer only uses binary, however if I want to write the machine
code to subtract 10 (decimal) from the value in the AL
register (Intel processor), 2C 0A is easier
to read and write (end less error prone!) than
00101100 00001010.
-
Hexadecimal is base 16; a single base 16 digit can represent 16
different base 16 numbers: 0 - 15 (in decimal); 0 - F (in hex).
-
From the previous section we know that 4 binary digits (bits) can
represent 16 different things or states (e.g. 16 different hexadecimal
numbers!)
Put the other way round; if I have 16 different things (e.g. 16
hexadecimal numbers...) I need 4 bits to count or represent them
since 24 = 16.
-
I can "map" each 4-bit chunk of a binary number to a single
hexadecimal digit:
Starting from the right (least significant digit, or "ones"
place), replace each set of 4 binary digits with 1 hexadecimal
digit.
-
Add leading 0's to left of the last chunk if it is less than 4
bits.
| 11110001011010
base 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 ]
ASCII Character Set
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
|
In computer memory the image would be a continuous "bit stream"
(shown here in 7-bit groups to represent the grid rows):
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.
-
How many bits per pixel are needed to represent 16 colors?
-
How many bits per pixel are needed to represent 256 colors?
[ TOP ]
- Instructions have an Operation Code (Opcode) and an
Operand
- Opcode says what to do
- Operand typically specifies the address to get the data
for the opcode, or specifies an immediate value to use.
- Given 8 bits (1 byte) each for opcode and operand, what are
the limits:
- How many instructions can we have?
- How many memory addresses can we access?
An Intel 8086 machine code instruction and its equivalent assembly
language instruction:
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 ]
Binary numbers in the computer:
| Memory: |
Low |
> > |
> > |
High |
| Binary |
00110101 |
00100001 |
01010001 |
01010000 |
| Hexadecimal |
35base 16 |
21base 16 |
51base 16 |
50base 16 |
| 8-bit integer |
53D ( 35H ) |
33D ( 21H ) |
81D ( 51H ) |
80D ( 50H ) |
| 16-bit integer (Intel) |
8501D ( 2135H ) |
20561D ( 5051H ) |
| 16-bit integer (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 ]
Revised: 31 JAN 2003 11:04