Lecture 2 - Number System
Lecture 2 - Number System
2
Decimal system
• We use decimal numbers everyday
• It is a base-10 system
• 10 symbols: 0,1, 2, 3, 4, 5, 6,7, 8, 9
• The position of each digit in a decimal
number can be assigned a weight
3
Decimal System
• Most significant digit (MSD) - the digit that carries the most weight,
usually the left most
• Least significant digit (LSD) - the digit that carries the least weight,
usually the right most
• Take example: decimal number 2745.214
2 7 4 5 . 2 1 4
4
Binary System
5
Binary System
• The position of each digit (bit) in a binary number can be
assigned a weight
• For example: 1011.101
– 1011.101 is a binary number
– 1 is a digit, 0 is a digit, 1 is a digit…
1 0 1 1 . 1 0 1
MSB LSB
6
Binary System (BIT)
• It takes more digits in the binary system to
represent the same value in the decimal
system.
• Examples: 710 = 1112
1010 102
• A single binary digit is referred to as a bit.
• 8 bits make a byte
7
Binary System (BIT)
• With N bits we have 2N discrete values.
• For example, a 4-bit system can represent
24 or 16 discrete values.
• The largest value is always 2N – 1.
• For the 4-bit system, 24 – 1 = 1510.
• The range of values for a 4-bit number is
then 0 thru 15.
8
Hexadecimal System
• Base-16 system
• 16 symbols: 10 numeric digits and 6
alphabetic characters
– 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
2 A F 8 . 9 8 E
10
Octal System
• Base-8 system
• 8 digits: 0, 1, 2, 3, 4, 5, 6, 7
• Convenient way to express binary
numbers and codes
11
Table of Number Systems
DECIMAL BINARY HEXADECIMAL OCTAL
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
8 1000 8 10
9 1001 9 11
10 1010 A 12
11 1011 B 13
12 1100 C 14
13 1101 D 15
14 1110 E 16
15 1111 F 17 12
Number Systems
13
Conversion between Numbers
5
Octal
6 (base 8)
9
10
Decimal 1 Binary
(base 10) 2 (base 2)
7
8
Hexadecimal
3 (base 16)
4
14
1) Decimal to Binary
• METHOD 1: Decimal number binary number
Method: sum-of-weights
Step 2: Subtract the power of two (from Step 1) from the given decimal
number.
Step 4: Write out the binary number based on all the powers of two from
Step 1.
16
Method 1: Sum of Weights
Example 1: Convert 2510 to binary
Step 3: Repeat Step 1: the power of two which is nearest to 9 but less than 9 is 2 3 = 8
Repeat Step 2: the result of subtraction 9 – 8 = 1
Repeat Step 1: the power of two which is nearest to 1 and equal to 1 is 2 0 = 1
Repeat Step 2: the result of subtraction 1 – 1 = 0
Step 4: Write out the binary number based on all the powers of two from Step 1.
Weights 24 23 22 21 20
Binary number 1 1 0 0 1 17
Method 2: Division-by-2
• Method 2 is use to convert only whole decimal
numbers (no fraction) to binary.
• Repeat the division of the decimal number with 2
until the quotient is 0.
• Remainder of each division determine the binary
number. First remainder represent the LSB and
the last remainder is the MSB.
18
Method 2: Division-by-2
Example 1: Convert 2510 to binary
Quotient Remainder
25
12 1
2 LSB
12
6 0
2
6
3 0
2
3 2510 = 1 1 0 0 1
1 1
2
1
0 1 MSB
2 19
Method 3: Multiplication-by-2
• Method 3 is use to convert decimal
fraction only to binary.
• Repeat the multiplication until the
fractional part of the product is all zeros.
• The binary number is determined by the
first digit in the multiplication results.
20
Method 3: Multiplication-by-2
Example 2: Convert 0.3125 to binary
Carry
0.50 x 2 = 1.00 1
21
2) Binary to Decimal
• Only one method is use. That is the sum of weight.
1 0 1 1 . 1 0 1
22
3) Decimal to Hexadecimal
• Method use is repeated division by-16.
• Repeat the division of the decimal number with
16 until the quotient is 0.
• Remainder of each division determine the hex
number. First remainder represent the LSB and
the last remainder is the MSB.
23
Decimal to Hexadecimal
Example 4: Convert 65010 to hex number
650 LSB
40 10 A
16
40
2 8 8
16
2
0 2 2
16
MSB
65010 = 2 8 A
24
4) Hexadecimal to Decimal
• Only one method is use. That is the sum of weight.
A 8 5
26
Decimal to Octal
Example 6: Convert 35910 to octal number
Quotient Remainder
359 LSB
44 7
8
44
5 4
8
5
0 5
8
MSB
35910 = 5 4 7
27
6) Octal to Decimal
• Only one method is use. That is the sum of weight.
83 82 81 80
2 3 7 4
hexadecimal 3 F 1 6 9
29
8) Hexadecimal to Binary
• Step: Replace each digit of the hexadecimal number
with the equivalent 4-bit binary number.
hexadecimal C F 8 E
30
9) Binary to Octal
• Step 1: Break the binary number into 3-bit groups, starting
from LSD.
• Step 2: Replace each 3-bit group with the equivalent octal
number.
octal 5 7 1
31
10) Octal to Binary
• Step: Replace each digit of the octal number with the
equivalent 3-bit binary number.
octal 7 5 2 6
32
Summary of Conversion
Division-by-8
Octal
Sum of weight (base 8)
3 bit conversion
Digit-to-3 bit
Division-by-2
Decimal Binary
(base 10) (base 2)
Sum of weight
4 bit conversion
Digit-to-4bit
Hexadecimal
Division-by-16 (base 16)
Sum of weight
33
Binary Codes
• Code Special group of symbols that is used to represent
numbers, letters or words.
• Encoding The process of converting a number/letter/word into a
code.
Number,
Letter, encoding Code
Word
• Codes discussed:
– BCD Code
– Gray Code
– ASCII Code
34
BCD Code
• Binary Coded Decimal Code
• Binary Coded Decimal (BCD): a way to
represent each digit of a decimal number with its
4-bit binary number.
Example 12 : Code the decimal number 87410 to a BCD Code
Decimal 8 7 4
35
1000 1111 is not a BCD
BCD Code
• Convert a BCD code to its decimal equivalent.
– Step 1: Break the BCD into 4-bit groups, starting from
LSB
– Step 2: Replace each 4-bit group with its equivalent
decimal
Example 13 : Convert the BCD code 0110 1000 0011 1001 to its decimal number.
6 8 3 9
9 1001 1 =0 1
2
The BCD code is 0001 0011 0111 The straight binary code is 10001001
37
Gray Code
DECIMAL BINARY GRAY CODE
• No weights assigned 0 0000 0000
in sequence. 7
8
0111
1000
0100
1100
1011001
processor
40
ASCII Code
• ASCII code has 128 characters and
symbols
• Represented by 7-bit binary code
• Can be considered an 8-bit code with MSB
0.
• The first 32 ASCII characters are non-
graphic commands only for control
purposes—The ASCII Control Characters.
• E.g.: null, line feed, start of text, escape
41
Binary Arithmetic
• Binary Addition
• Binary Subtraction
• Binary Multiplication
• Binary Division
42
Binary Addition
• Two binary numbers are added by adding each
pair of bits together with carry propagation.
0+0=0 with a carry of 0
0+1=1 with a carry of 0
1+0=1 with a carry of 0
1 + 1 = 10 with a carry of 1
43
Binary Addition
Example 15:
1 0 0 0 8
+ 1 1 1 + 7
1 1 1 1 15
Example 16:
carry 1 1
1 1 3
+ 1 1 + 3
1 1 0 6
44
Binary Subtraction
• Two binary numbers are subtracted by
subtracting each pair of bits together with
borrowing, if needed.
0–0=0
1–1=0
1–0=1
10 – 1 = 1
45
Binary Subtraction
Example 17:
1 0 1 5
+ 1 1 - 3
1 0 2
Example 18:
borrow 0 1 1 0 1 1 0 1
1 1 0 1 0 0 1 0 210
- 0 1 1 0 1 1 0 1 - 109
0 1 1 0 0 1 0 1 101
46
Binary Multiplication
• The procedure is same as decimal
multiplication
0x0=0
0x1=0
1x0=0
1x1=1
47
Binary Multiplication
Example 19:
1 0 1 5
x 1 1 1 x 7
1 0 1 35
1 0 1
1 0 1
1 0 0 0 1 1
48
Binary Division
• The procedure is same as decimal division. For the sake
of simplicity, throw away the remainder.
Example 20:
1 0 2
1 1 1 1 0 3 6
1 1 6
0 0
49
Negative Numbers
• Computer must be handle both positive
and negative numbers.
• A signed binary number consists of both
sign and magnitude information.
• 3 types of representation:
Sign and magnitude (least used)
1’s complement
2’s complement (most important)
50
Sign and Magnitude
• The sign bit, i.e. the left-most bit in a
signed binary number
– A ‘0’ sign bit indicates a positive number
– A ‘1’ sign bit indicates a negative number
51
Sign and Magnitude
Example 21 : Express the decimal number -39 as an 8-bit number in the
sign-magnitude.
52
Sign and Magnitude
-7 +0
-6 1111 0000 +1
1110 0001
-5 +2 +
1101 0010
-4 1100 0011 +3 0 100 = + 4
53
1’s Complement
• To find the 1’s complement of a given binary
number,
– Change all 1s to 0s and all 0s to 1s.
1 0 1 1 0 0 1 0
0 1 0 0 1 1 0 1 1’s complement
54
1’s Complement
-0 +0
-1 1111 0000 +1
1110 0001
-2 +2 +
1101 0010
-3 1100 0011 +3 0 100 = + 4
55
2’s Complement
• To find the 2’s complement of a given binary
number,
– Add 1 to the LSB of the 1’s complement
1 0 1 1 0 0 1 0 Binary number
0 1 0 0 1 1 0 1 1’s complement
+ 1 add 1
0 1 0 0 1 1 1 0 2’s complement
56
2’s Complement
-1 +0
-2 1111 0000 +1
1110 0001
-3 +2 +
1101 0010
-4 1100 0011 +3 0 100 = + 4
+19 -19
58
Comparison
59