Number Systems

Download Report

Transcript Number Systems

Number Systems
1
Numbers
•
Each number system is associated with a base or radix
– The decimal number system is said to be of base or radix 10
•
A number in base r contains r digits 0,1,2,...,r-1
– Decimal (Base 10): 0,1,2,3,4,5,6,7,8,9
•
Numbers are usually expressed in positional notation
– MSD: most significant digit
– LSD: least significant digit
2
Numbers
•
The value of the number is given in the polynomial form
3
Numbers
In addition to decimal, three other number systems are
also important: Binary, Octal, and Hexadecimal
4
Unsigned Binary Numbers
•
The binary number system: Base-2
• Two digits: 0 and 1
•
The digits in a binary number are called bits
–
–
MSB: most significant bit
LSB: least significant bit
5
Unsigned Binary Numbers
6
Unsigned Binary Numbers
•
For a computer with the word size of 32-bit
- 4 data-bit unit – nibble (half byte)
-
8 data-bit unit - byte
16 data-bit unit – two bytes (half-word)
32 data-bit unit – word (four bytes)
64 data-bit unit – double-word
Powers of 2:
20 = 1
21 = 2
22 = 4
23 = 8
24
25
26
27
=
=
=
=
16
32
64
128
28 = 256
29 = 512
210 = 1024
210 : K (kilo); 220 : M (mega); 230 : G( giga )
7
Converting Binary to Decimal
•
For example, here is 1101.01 in binary:
1
23
1
22
0
21
1
20
.
0
2-1
1
2-2
Bits
Weights (in base 10)
(1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) =
8
+
4
+
0
+
1
+
0
+
0.25
= 13.25
(1101.01)2 = (13.25)10
8
Converting Decimal to Binary
•
•
•
•
To convert a decimal integer into binary, keep dividing by
2 until the quotient is 0. Collect the remainders in
reverse order
To convert a fraction, keep multiplying the fractional
part by 2 until it becomes 0. Collect the integer parts in
forward order
Example: 162.375:
So, (162.375)10 = (10100010.011)2
162 / 2
81 / 2
40 / 2
20 / 2
10 / 2
5/2
2/2
1/2
= 81
= 40
= 20
= 10
=5
=2
=1
=0
rem
rem
rem
rem
rem
rem
rem
rem
0
1
0
0
0
1
0
1
0.375 x 2 = 0.750
0.750 x 2 = 1.500
0.500 x 2 = 1.000
9
Why does this work?
•
•
This works for converting from decimal to
any base
Why? Think about converting 162.375 from
decimal to decimal
162 / 10 = 16
16 / 10 = 1
1 / 10 = 0
•
•
rem 2
rem 6
rem 1
0.375 x 10 = 3.750
0.750 x 10 = 7.500
0.500 x 10 = 5.000
Each division strips off the rightmost digit
(the remainder). The quotient represents
the remaining digits in the number
Similarly, to convert fractions, each
multiplication strips off the leftmost digit
(the integer part). The fraction
represents the remaining digits
10
Octal and Hexadecimal Numbers
• The octal number system: Base-8
• Eight digits: 0,1,2,3,4,5,6,7
(127.4)8  1 82  2  81  7  80  4  81  (87.5)10
•
•
•
The hexadecimal number system: Base-16
Sixteen digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
For our purposes, base-8 and base-16 are most useful as
a “shorthand” notation for binary numbers
( B65F )16  11163  6 16 2  5 161  15 16 0  (46687)10
11
Numbers with Different Bases
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Octal
0
1
2
3
4
5
6
7
10
11
12
13
14
15
16
17
Hex
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
You can convert between base-10
base-8 and base-16 using techniques
like the ones we just showed for
converting between decimal and
binary
12
Binary and Octal Conversions
• Converting from octal to binary: Replace each octal digit with its
equivalent 3-bit binary sequence
(673.12)8
=
=
=
6
110
7
111
3
011
.
1
. 001
2
010
(110111011.001010)2
• Converting from binary to octal: Make groups of 3 bits, starting
from the binary point. Add 0s to the ends of the number if
needed. Convert each bit group to its corresponding octal digit.
10110100.0010112 = 010
=
2
Octal
0
1
2
3
Binary
000
001
010
011
110
6
100 .
4
.
Octal
4
5
6
7
001
1
Binary
100
101
110
111
0112
38
13
Binary and Hex Conversions
• Converting from hex to binary: Replace each hex digit with its
equivalent 4-bit binary sequence
261.3516 =
2
6
1
.
3
516
= 0010 0110 0001 . 0011 01012
• Converting from binary to hex: Make groups of 4 bits, starting
from the binary point. Add 0s to the ends of the number if needed.
Convert each bit group to its corresponding hex digit
10110100.0010112 = 1011
=
B
Hex Binary
0
0000
1
0001
2
0010
3
0011
Hex Binary
4
0100
5
0101
6
0110
7
0111
0100 . 0010 11002
4
.
2
C16
Hex
8
9
A
B
Binary
1000
1001
1010
1011
Hex Binary
C
1100
D
1101
E
1110
F
1111
14
Unsigned Binary Coded Decimal (BCD)
15
Number Systems Summary
•
•
Computers are binary devices
– We’re forced to think in terms of base 2.
– We learned how to convert numbers between binary, decimal,
octal and hexadecimal
We’ve already seen some of the recurring themes of
architecture:
– We use 0 and 1 as abstractions for analog voltages.
– We showed how to represent numbers using just these two
signals.
16