Number Systems and Codes
1. Decimal, Binary, Octal & Hexadecimal
2. Conversion between numbers
3. Binary Codes
4. Binary Arithmetic
5. Negative Numbers
Digital number systems
• Many number systems are used in digital
electronics.
– Decimal number system
– Binary number system
– Octal number system
– Hexadecimal 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
weights 103 102 101 100 10-1 10-2 10-3
2 7 4 5 . 2 1 4
2 10 3 7 10 2 4 101 5 10 0 2 10 1 1 10 2 4 10 3
4
Binary System
• Difficult to design a system that works
with 10 different voltage levels
• Base-2 system
• 2 digits/symbols: 0, 1
• Examples: 0, 1, 01, 111, 101010
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…
weights 23 22 21 20 2-1 2-2 2-3
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
• Compact way of writing binary number
• Widely used in computer and
microprocessor applications
9
Hexadecimal System
• Examples: 1C16 , A8516
• The position of each digit in a hexadecimal
number can be assigned a weight
• For example: 2AF8.98E
weights 163 162 161 160 16-1 16-2 16-3
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
• METHOD 2: Decimal whole number binary
number
Method: division-by-2
• METHOD 3: Decimal fraction binary number
Method: multiplication-by-2
15
Method 1: Sum of Weights
Step 1: Find the power of two that fulfills the following:
a. nearest to the given decimal number; and
b. its decimal number is less than or equal to the given decimal number.
Step 2: Subtract the power of two (from Step 1) from the given decimal
number.
Step 3: If the result of the subtraction in Step 2 is 0, go to Step 4. Else,
repeat Steps 1 and 2 for the result of the subtraction in Step 2.
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 1: 22 = 4? No, because it is not the nearest.
23 = 8 is nearer and still less than 25.
24 = 16 is the nearest and its decimal, 16 is less than 25.
25 = 32 No, because its decimal number, 32 is more than 25.
Step 2: the result of subtraction 25 – 16 = 9
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.3125 x 2 = 0.625 0 The
binary
MSB fraction
0.625 x 2 = 1.25 1 is:
. 0 1 0 1
0.25 x 2 = 0.50 0
LSB
0.50 x 2 = 1.00 1
21
2) Binary to Decimal
• Only one method is use. That is the sum of weight.
Example 3: Convert 1011.101 to decimal
23 22 21 20 2-1 2-2 2-3
1 0 1 1 . 1 0 1
=(1x23) + (0x22) + (1x21) + (1x20) + (1x2-1) + (0x2-2) + (1x2-3)
= 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125
= 11.62510
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
Quotient Remainder Remainder
(decimal) (hexadecimal)
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.
Example 5: Convert A8516 to decimal number
162 161 160
A 8 5
= (A x 162) + (8 x 161) + (5 x 160)
= (10 x 256) + (8 x 16) + (5 x 1)
= 2560 + 128 + 5
= 2693
25
5) Decimal to Octal
• Method use is repeated division by-8.
• Repeat the division of the decimal number
with 8 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.
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.
Example 7: Convert 23748 to decimal number
83 82 81 80
2 3 7 4
= (2 x 83) + (3 x 82) + (7 x 81) + (4 x 80)
= (2 x 512) + (3 x 64) + (7 x 8) + (4 x 1)
= 1024 + 192 + 56 + 4
= 1276
28
7) Binary to Hexadecimal
Step 1: Break the binary number into 4-bit groups, starting
from LSB.
Step 2: Replace each 4-bit with the equivalent hexadecimal
number.
Example 8: Convert 111111000101101001 to hex number
binary 0011 1111 0001 0110 1001
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.
Example 9: Convert CF8E16 to binary number
hexadecimal C F 8 E
binary 1100 1111 1000 1110
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.
Example 10: Convert 1011110012 to octal number
binary 101 111 001
octal 5 7 1
31
10) Octal to Binary
• Step: Replace each digit of the octal number with the
equivalent 3-bit binary number.
Example 11: Convert 75268 to binary number
octal 7 5 2 6
binary 111 101 010 110
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
BCD 1000 0111 0100
Therefore, the BCD code for 87410 is 1000 0111 0100
Do you know why?
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.
0110 1000 0011 1001
6 8 3 9
So, the decimal equivalent is 683910
36
BCD coding vs. Straight Binary coding
• BCD coding is easier and straight forward.
Example 14 : Convert the decimal number 137.
137 = 68 1
2
Decimal Binary
1 3 7 68 = 34 0
0 0000 2
1 0001 34 = 17 0
2
2 0010 0001 0011 0111 17 =8 1
3 0011 2
4 0100 8 =4 0
2
5 0101
4 =2 0
6 0110 2
7 0111 2 =1 0
8 1000 2
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
to the bit positions 1 0001 0001
2 0010 0011
• Only a single bit 3 0011 0010
4 0100 0110
change from one 5 0101 0111
code word to the next 6 0110 0101
in sequence. 7
8
0111
1000
0100
1100
• Good minimize the 9
10
1001
1010
1101
1111
chance for error. 11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000 38
Alphanumeric codes
• Codes that represent numbers and
alphabetic characters (letters).
• At minimum, the code must represent 10
decimal digits (0-9) and 26 letters (A-Z).
• 6 bits are needed in the code that
represent the numbers and letters.
• ASCII is the most common alphanumeric
code.
39
ASCII Code
• American Standard Code for International
Interchange
• Used in computers (keyboard and printers)
and electronic equipment
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
• The remaining bits are the magnitude bits.
51
Sign and Magnitude
Example 21 : Express the decimal number -39 as an 8-bit number in the
sign-magnitude.
First: Convert 3910 to binary =1001112
Second: Add a zero to as the 7th bit = 01001112
Since the decimal is a negative number, the sign bit is 1.
Therefore, -3910=101001112
52
Sign and Magnitude
-7 +0
-6 1111 0000 +1
1110 0001
-5 +2 +
1101 0010
-4 1100 0011 +3 0 100 = + 4
-3 1011 0100 +4 1 100 = - 4
1010 0101
-2 +5 -
1001 0110
-1 1000 0111 +6
-0 +7
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.
Example 22 : Find the 1’s complement of 10110010
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
-4 1011 0100 +4 1 011 = - 4
1010 0101
-5 +5 -
1001 0110
-6 1000 0111 +6
-7 +7
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
Example 23 : Find the 2’s complement of 10110010
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
-5 1011 0100 +4 1 100 = - 4
1010 0101
-6 +5 -
1001 0110
-7 1000 0111 +6
-8 +7
• Only one representation for 0
• One more negative number than positive number 57
Comparison
Example 24: Express the decimal numbers +19 and -19 as an 8-bit number in
the sign-magnitude, 1’s complement and 2’s complement forms.
+19 -19
Sign-magnitude 00010011 10010011
1’s complement 00010011 11101100
2’s complement 00010011 11101101
Positive number remain the same
58
Comparison
59