Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Ece 301 lecture 2 - number systems and codes
1. NUMBER SYSTEMS
ECE 301 – Digital Electronics
Dr. Xiang Chen
Electrical and Computer Engineering
Lecture #2
Spring 2019 ECE 301 - Digital Electronics 1
2. Topics Covered
• Binary, Hexadecimal, and Octal number systems.
• Number system conversion
• Binary to decimal.
• Decimal to binary.
• Binary to hexadecimal.
• Hexadecimal to binary.
• Binary Codes
• 8-4-2-1 Weighted
• Binary Coded Decimal (BCD), Excess-3, Gray
• ASCII
• Parity
Spring 2019 ECE 301 - Digital Electronics 2
5. Positional Number Systems
Spring 2019 ECE 301 - Digital Electronics 5
[ kn-1kn-2…k1k0 . k-1k-2…k-(m-1)k-m ]r
n-digit integer m-digit fraction
radix point radix (base)
V(K) = kn-1 x rn-1 + kn-2 x rn-2 + … + k1 x r1 + k0 x r0
+ k-1 x r-1 + k-2 x r-2 + … + k-m x r-m
The weight of each digit is implied by its position in the number.
6. Number Systems
• The number systems of significance to this course are:
Spring 2019 ECE 301 - Digital Electronics 6
Number System Base Digits
Binary 2 0, 1
Octal 8 0 – 7
Decimal 10 0 – 9
Hexadecimal 16 0 – 9, A – F
7. Number Systems: Decimal
• Radix (base) = 10
• Digits: D = { 0, 1, … , 8, 9 }
• D = dn-1dn-2…d0.d-1d- 2…d-m
• D = 492.3710
• V(D) = 4 x 102
+ 9 x 101
+ 2 x 100
+ 3 x 10-1
+ 7 x 10-2
Spring 2019 ECE 301 - Digital Electronics 7
V(D) = dn-1 x 10n-1 + dn-2 x 10n-2 + … + d1 x 101 + d0 x 100
+ d-1 x 10-1 + d-2 x 10-2 + … + d-m x 10-m
integer fraction
decimal point
8. Number System: Binary
• Radix (base) = 2
• Digits: B = { 0, 1 }
• B = bn-1bn-2…b0.b-1b- 2…b-m
• B = 10110.0112
• V(B) = 1 x 24
+ 0 x 23
+ 1 x 22
+ 1 x 21
+ 0 x 20
+ 0 x 2-1
+ 1 x 2-2
+ 1 x 2-3
V(B) = 22.37510
Spring 2019 ECE 301 - Digital Electronics 8
integer fraction
binary point
V(B) = bn-1 x 2n-1 + bn-2 x 2n-2 + …
+ b1 x 21 + b0 x 20
+ b-1 x 2-1 + b-2 x 2-2 + … + b-m x 2-m
base
An n-bit integer can represent a value in the range: 0 to 2n-1.
LSbMSb
9. Number Systems: Hexadecimal
• Radix (base) = 16
• Digits: H = { 0, 1, … , 9, A, …, F }
• A = 10, B = 11, C = 12
• D = 13, E = 14, F = 15
• H = hn-1hn-2…h0.h-1h- 2…h-m
• H = B7E.3C16
• V(H) = 11 x 162
+ 7 x 161
+ 14 x 160
+ 3 x 16-1
+ 12 x 16-2
V(H) = 2942.2310
Spring 2019 ECE 301 - Digital Electronics 9
V(H) = hn-1 x 16n-1 + hn-2 x 16n-2 + … + h1 x 161 + h0 x 160
+ h-1 x 16-1 + h-2 x 16-2 + … + h-m x 16-m
integer fraction
hexadecimal point
base
10. Conversion: Decimal to Binary
• To convert the integer part of a fixed point decimal number.
• Use repeated division.
• Conversion from decimal (r = 10) to binary (r = 2):
(DINT)10 = bn-1 x 2n-1 + bn-2 x 2n-2 + … + b1 x 21 + b0 x 20
(DINT)10 / 2 = Q0 + remainder
= Q0 + b0
(Q0) / 2 = Q1 + b1
(Q1) / 2 = Q2 + b2
...
Spring 2019 ECE 301 - Digital Electronics 10
LSb
12. Conversion: Decimal to Binary
• To convert the integer part of a fixed-point decimal number.
• Alternately, use repeated subtraction.
• Conversion from decimal (r = 10) to binary (r = 2):
(DINT)10 = bn-1 x 2n-1 + bn-2 x 2n-2 + … + b1 x 21 + b0 x 20
Spring 2019 ECE 301 - Digital Electronics 12
Largest power of 2 that is less than DINT.
13. Conversion: Decimal to Binary
Spring 2019 ECE 301 - Digital Electronics 13
(157)10
157 – 128 = 29 1 x 27
29 – 16 = 13 1 x 24
13 – 8 = 5 1 x 23
5 – 4 = 1 1 x 22
1 – 1 = 0 1 x 20
= (10011101)2
LSb
MSb
14. Conversion: Decimal to Binary
• To convert the fraction part of a fixed-point decimal number.
• Use repeated multiplication.
• Conversion from decimal (r = 10) to binary (r = 2):
(DFRAC)10 = b-1 x 2-1 + b-2 x 2-2 + … + b-(m-1) x 2-(m-1) + b-m x 2-m
(DFRAC)10 * 2 = b-1 + R0
R0 * 2 = b-2 + R1
R1 * 2 = b-3 + R2
…
Spring 2019 ECE 301 - Digital Electronics 14
16. Number Systems: Hexadecimal
• Hexadecimal is a useful shorthand notation for binary.
• Each hexadecimal digit corresponds to four bits.
Spring 2019 ECE 301 - Digital Electronics 16
Hexadecimal Decimal Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
Hexadecimal Decimal Binary
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
17. Conversion: Hexadecimal to Binary
• Convert each hex digit to an equivalent 4-bit binary number.
• Use the 8-4-2-1 binary weighted code.
• Repeat for all digits in the hexadecimal number.
Spring 2019 ECE 301 - Digital Electronics 17
4 C 9 . 1 B 16
0100 1100 1001 0001 1011
4C9.1B16 = 0100 1100 1001 . 0001 1011 2
18. Conversion: Binary to Hexadecimal
• Separate the binary number into 4-bit groups.
• Start at the binary point and work outwards.
• Pad with 0’s on the left (integer) and on the right (fraction).
• Convert each 4-bit group to the equivalent hex digit.
• Use the 8-4-2-1 weighted binary code.
• Repeat for all 4-bit groups in the binary number.
Spring 2019 ECE 301 - Digital Electronics 18
110 1000 1110 . 0111 101 2
6 8 E 7 A
Pad with 0’s Pad with 0’s
110 1000 1110 . 0111 101 2 = 68E.7A16
19. CODES
Binary codes (BCD, Excess-3, Gray, One-hot)
ASCII
Parity
Spring 2019 ECE 301 - Digital Electronics 19
20. Binary Coded Decimal (BCD)
• Each decimal digit is represented by a 4-bit pattern (or code).
• Only 10 of the 16 patterns are used.
• Remaining 6 patterns should never occur.
• Treated as “don’t cares”.
• Weighted Code:
• Each bit position has a specific weight (8-4-2-1).
• Decimal value of each code can be determined.
• Very different from binary representation.
• Advantage: easily displayed on simple
digit-oriented (7-Segment) displays.
• Disadvantage: complex arithmetic circuits.
Spring 2019 ECE 301 - Digital Electronics 20
Decimal BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
8-4-2-1 Code
21. Conversion: Decimal to BCD
• Convert each decimal digit to its 4-bit binary code.
• Use the 8-4-2-1 binary weighted code.
• Repeat for all digits in the decimal number.
Spring 2019 ECE 301 - Digital Electronics 21
9 3 7 . 2 5 10
1001 0011 0111 0010 0101
937.2510 = 1001 0011 0111 . 0010 0101 BCD
Binary Representation: 937.2510 = 1110101001.012
22. Conversion: BCD to Decimal
• Separate the BCD number into 4-bit groups.
• Start at the binary point and work outwards.
• Pad with 0’s on the left and on the right.
• Convert each 4-bit group to the equivalent decimal digit.
• Use the 8-4-2-1 weighted binary code.
• Repeat for all 4-bit groups in the BCD number.
Spring 2019 ECE 301 - Digital Electronics 22
0100 1001 0010 . 0101 0011 BCD
4 9 2 5 3
0100 1001 0010 . 0101 0011 BCD = 492.5310
23. Excess-3 Code
• Each decimal digit is represented by a 4-bit pattern (or code).
• Only 10 of the 16 patterns are used.
• Remaining 6 patterns should never occur.
• Treated as “don’t cares”.
• Based on the 8-4-2-1 weighted code.
• 8421 weighted code + 3
• Very different from binary representation.
Spring 2019 ECE 301 - Digital Electronics 23
Decimal Excess-3
0 0011
1 0100
2 0101
3 0110
4 0111
5 1000
6 1001
7 1010
8 1011
9 1100
24. Gray Code
• Each decimal digit is represented by a 4-bit pattern (or code).
• Only 10 of the 16 patterns are used.
• Remaining 6 patterns should never occur.
• Treated as “don’t cares”.
• Unweighted Code:
• Bit positions do not have specific weights.
• Decimal value assigned to each pattern/code.
• Very different from binary representation.
Spring 2019 ECE 301 - Digital Electronics 24
Decimal Gray Code
0 0000
1 0001
2 0011
3 0010
4 0110
5 1110
6 1010
7 1011
8 1001
9 1000
25. ASCII
• American Standard Code for Information Interchange
• Commonly used for storage and transfer of alphanumeric info.
• 7-bit weighted code used to represent 128 characters.
• Used for letters, numbers, and other characters.
• An eighth bit is often used for parity.
• Any word or number can be represented using ASCII.
Spring 2019 ECE 301 - Digital Electronics 25
HeLp!
Binary:
Hexadecimal: 0x48 0x65 0x4C 0x70
0100 1000 0110 0101 0100 1100 0111 0000
ASCII is a 7-bit Code.
MSb is assumed to be 0.
30. Binary: Powers of Two
Spring 2019 ECE 301 - Digital Electronics 30
Power of Two Decimal Value
20 1
21 2
22 4
23 8
24 16
25 32
26 64
27 128
28 256
29 512
210 1024
34. Binary: Number of Values and Range
• For an N-bit unsigned binary number,
• Number of values that can be represented: 2N
• Range of values: 0 <= V(B) <= 2N – 1
Spring 2019 ECE 301 - Digital Electronics 34
N # of Values Range
4 16 0 <= V(B) <= 15
5 32 0 <= V(B) <= 31
8 256 0 <= V(B) <= 255
16 65536 0 <= V(B) <= 65535
32 232 0 <= V(B) <= 232 – 1
35. Number Systems: Octal
• Radix (base) = 8
• Digits: O = { 0, 1, … , 6, 7 }
• O = on-1on-2…o0.o-1o- 2…o-m
• O = 2374.158
• V(O) = 2 x 83
+ 3 x 82
+ 7 x 81
+ 4 x 80
+ 1 x 8-1
+ 5 x 8-2
V(O) = 1276.2010
Spring 2019 ECE 301 - Digital Electronics 35
V(O) = on-1 x 8n-1 + on-2 x 8n-2 + … + o1 x 81 + o0 x 80
+ o-1 x 8-1 + o-2 x 8-2 + … + o-m x 8-m
integer fraction
octal point
base
36. Number Systems: Octal
• Octal was a useful shorthand notation for binary.
• Each octal digit corresponds to three bits.
Spring 2019 ECE 301 - Digital Electronics 36
Octal Decimal Binary
0 0 000
1 1 001
2 2 010
3 3 011
4 4 100
5 5 101
6 6 110
7 7 111
37. Conversion: Binary to/from Octal
• Convert the given octal numbers to binary:
101100010001.1110112
101 100 010 001 . 111 011 2 = 5421.738
1111010110101.01112
001 111 010 110 101 . 011 100 2 = 17265.348
• Convert the given binary number to octal:
341.528 = 011 100 001 . 101 010 2
Spring 2019 ECE 301 - Digital Electronics 37
38. Conversion: Binary to Octal
• Separate the binary number into 3-bit groups.
• Start at the binary point and work outwards.
• Pad with 0’s on the left (integer) and on the right (fraction).
• Convert each 3-bit group to the equivalent octal digit.
• Use the 4-2-1 weighted binary code.
• Repeat for all 3-bit groups in the binary number.
Spring 2019 ECE 301 - Digital Electronics 38
1 111 010 110 101 . 011 1 2
1 7 2 5 3
Pad with 0’s Pad with 0’s
1111010110101 . 0111 2 = 17265.348
6 4
39. Conversion: Octal to Binary
• Convert each octal digit to an equivalent 3-bit binary number.
• Use the 4-2-1 binary weighted code.
• Repeat for all digits in the octal number.
Spring 2019 ECE 301 - Digital Electronics 39
3 4 1 . 5 2 8
011 100 001 101 010
341.528 = 011 100 001 . 101 010 2
41. Conversion: Decimal to Base-r
• To convert a fixed-point decimal number to any other base:
1. Use repeated division to convert integer part.
• Divide by new base.
2. Use repeated multiplication to convert fraction part.
• Multiply by new base.
3. Combine integer and fraction parts in new base.
Spring 2019 ECE 301 - Digital Electronics 41
[ kn-1kn-2…k1k0 . k-1k-2…k-(m-1)k-m ]10
n-digit integer m-digit fraction