You are on page 1of 92

Digital Electronics

Chapter 2: Number systems and codes


Number systems

Why do people
think of
quadratic
codes?

Why do people think of decimal codes?

Why do people think binary code?


Number systems(1)
◼ Positional Notation
N = (an-1an-2 ... a1a0 . a-1a-2 ... a-m)r (1.1)
where . = Radix point
r = Radix or base
n = number of integer digits to the left of the radix point
m = number of fractional digits to the right of the radix point
an-1 = most significant digit (MSD)
a-m = least significant digit (LSD)

◼ Polynomial Notation (Series Representation)


N = an-1 x rn-1 + an-2 x rn-2 + ... + a0 x r0 + a-1 x r-1 ... + a-m x r-m
n −1
=  ai r i (1.2)
i = −m

◼ N = (251.41)10 = 2 x 102 + 5 x 101 + 1 x 100 + 4 x 10-1 + 1 x 10-2


Number Systems (2)
◼ Binary numbers
 Digits = {0, 1}
 (11010.11)2 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20
+ 1 x 2-1 + 1 x 2-2
= (26.75)10

1 K (kilo) = 210 = 1,024,


 1M (mega) = 220 = 1,048,576,
 1G (giga) = 230 = 1,073,741,824
Number Systems (3)
◼ Octal numbers
 Digits = {0, 1, 2, 3, 4, 5, 6, 7}
 (127.4)8 = 1 x 82 + 2 x 81 + 7 x 80 + 4 x 8-1 = (87.5)10

◼ Hexadecimal numbers
 Digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}
 (B65F)16 = 11 x 163 + 6 x 162 + 5 x 161 + 15 x 160 =
(46,687)10

5 Chapter 1
Number Systems (3)
Base Conversion (1)
◼ Series Substitution Method
 Expanded form of polynomial representation:
N = an-1rn-1 + … + a0r0 + a-1r-1 + … + a-mr-m (1.3)
 Conversation Procedure (base A to base B)
◼ Represent the number in base A in the format of Eq. 1.3.

◼ Evaluate the series using base B arithmetic.

 Examples:
◼ (11010)2 →( ? )10

N = 1´24 + 1´23 + 0´22 + 1´21 + 0´20


= (16)10 + (8)10 + 0 + (2)10 + 0
= (26)10
◼ (627)8 → ( ? )10

N = 6´82 + 2´81 + 7´80


= (384)10 + (16)10 + (7)10
= (407)10
Base Conversion (2)
◼ Radix Divide Method
 Used to convert the integer in base A to the equivalent base B
integer.
 Underlying theory:
◼ (NI)A = bn-1Bn-1 + … + b0B0 (1.4)
Here, bi’s represents the digits of (NI)B in base A.
◼ NI / B = (bn-1Bn-1 + … + b1B1 + b0B0 ) / B

= (Quotient Q1: bn-1Bn-2 + … + b1B0 ) + (Remainder R0:


b0)
◼ In general, (bi)A is the remainder Ri when Qi is divided by (B)A.

 Conversion Procedure
1. Divide (NI)B by (B)A, producing Q1 and R0. R0 is the least
significant digit, d0, of the result.
2. Compute di, for i = 1 … n - 1, by dividing Qi by (B)A, producing
Qi+1 and Ri, which represents di.
3. Stop when Qi+1 = 0.
Number Systems (4)
◼ Base Conversion

Convert decimal to binary Convert binary to decimal


Base Conversion (4)
 Examples
◼ (315)10 = (473)8
8 315 3 LSD
8 39 7
8 4 4 MSD
0

◼ (315)10 = (13B)16
16 315 B LSD
16 19 3
16 1 1 MSD
0
Base Conversion (5)
◼ Radix Multiply Method
 Used to convert fractions.
 Underlying theory:
◼ (NF)A = b-1B-1 + b-2B-2 + … + b-mB-m (1.5)
Here, (NF)A is a fraction in base A and bi’s are the digits of (NF)B in
base A.
◼ B ´ NF = B ´ (b-1B-1 + b-2B-2 + … + b-mB-m )

= (Integer I-1: b-1) + (Fraction F-2: b-2B-1 + … + b-mB-(m-1))


◼ In general, (bi)A is the integer part I-i, of the product of F-(i+1) ´ (BA).

 Conversion Procedure
1. Let F-1 = (NF)A.
2. Compute digits (b-i)A, for i = 1 … m, by multiplying Fi by (B)A,
producing integer I-i, which represents (b-i)A, and fraction F-(i+1).
3. Convert each digits (b-i)A to base B.
Base Conversion (5)
 Examples
◼ (0.479)10 = (0.3651…)8

MSD 3.832  0.479 ´ 8


6.656  0.832 ´ 8
5.248  0.656 ´ 8
LSD 1.984  0.248 ´ 8

◼ (0.479)10 = (0.0111…)2
MSD 0.9580  0.479 ´ 2
1.9160  0.9580 ´ 2
1.8320  0.9160 ´ 2
LSD 1.6640  0.8320 ´ 2

Base Conversion (6)
◼ When B = Ak
◼ Algorithm 1.3
(a) To convert a number N from base A to base B when B = Ak and k is a
positive integer, group the digits of N in groups of k digits in both directions
from the radix point and then replace each group with the equivalent digit in
base B
(b) To convert a number N from base B to base A when B = Ak and k is a
positive integer, replace each base B digit in N with the equivalent k digits in
base A.

◼ Examples
 (001 010 111. 100)2 = (127.4)8 (group bits by 3)
 (1011 0110 0101 1111)2 = (B65F)16 (group bits by 4)

13 Chapter 1
Base Conversion (6)
◼ Question: How to convert octal
and hexadecimal to binary and
vice versa?
Arithmetic (1)
◼ Binary Arithmetic
 Addition

111011 Carries
101011 Augend
+ 11001 Addend
1000100

 Subtraction

0 1 10 0 10 Borrows
1 0 0 1 0 1 Minuend
- 1 1 0 1 1 Subtrahend
1 0 1 0
Arithmetic (2)
◼ Performs the following binary
operations::

11 0 1 1
+ 1 0 1 0
Result?

1 0 0 0 1 0 0
- 1 0 1 0 1 1
Result?

1 0 0 1 0 0
- 1 1 0 1 1
Result?
Arithmetic (3)
 Multiplication

1 1 0 1 0 Multiplicand
x 1 0 1 0 Multiplier
0 0 0 0 0 Partial products
1 1 0 1 0
0 0 0 0 0
1 1 0 1 0
1 0 0 0 0 0 1 0 0 Product

17
Arithmetic (4)
 Division

Dividend Divider

Quotient

Remainder
Arithmetic (5)

Application of shift register in


multiplication and division by 2
Signed Number Representation
◼ Signed Magnitude Method
 N = ± (an-1 ... a0.a-1 ... a-m)r is represented as
N = (san-1 ... a0.a-1 ... a-m)rsm,
where s = 0 if N is positive and s = r -1 otherwise.
 N = -(15)10
 In binary: N = -(15)10 = -(1111)2 = (1, 1111)2sm
 In decimal: N = -(15)10 = (9, 15)10sm
Complementary Number Systems (1)
◼ Complementary Number Systems
 Radix complements (r's complements)
[N]r = rn - (N)r
where n is the number of digits in (N)r.

 Diminished radix complements (r-1’s complements)


[N]r-1 = rn - (N)r - 1
Radix Complement Number Systems (2)
◼ Two's complement of (N)2 = (101001)2
[N]2 = 26 - (101001)2 = (1000000)2 - (101001)2 = (010111)2

◼ (N)2 + [N]2 = (101001)2 + (010111)2 = (1000000)2


If we discard the carry, (N)2 + [N]2 = 0.
Hence, [N]2 can be used to represent -(N)2.

◼ [ [N]2 ]2 = [(010111)2]2 = (1000000)2 - (010111)2 = (101001)2 = (N)2.

◼ Two's complement of (N)2 = (1010)2 for n = 6


[N]2 = (1000000)2 - (1010)2 = (110110)2.

◼ Ten's complement of (N)10 = (72092)10


[N]10 = (100000)10 - (72092)10 = (27908)10.
Radix Complement Number Systems (3)
◼ Algorithm Find [N]r given (N)r .
 First replace each digit, ak , of (N)r by (r - 1) - ak and then add 1 to the
resultant.
◼ For binary numbers (r = 2), complement each digit and add 1 to the result.

◼ Example: Find 2’s complement of N = (01100101)2 .


N = 01100101
10011010 Complement the bits
+1 Add 1
[N]2 = (10011011)10
◼ Example: Find 10’s complement of N = (40960)10
N = 40960
59039 Complement the bits
+1 Add 1
[N]2 = (59040)10
Radix Complement Number Systems (4)
◼ Two's complement number system:
 Positive number :
◼ N = +(an-2, ..., a0)2 = (0, an-2, ..., a0)2cns,
n −1
where 0  N  2 − 1.
 Negative number:
◼ N = (an-1, an-2, ..., a0)2

◼ -N = [an-1, an-2, ..., a0]2 (two's complement of N),

where − 1  N  −2 n −1.

 Example: Two's complement number system representation of ± (N)2


when (N)2 = (1011001)2 for n = 8:
◼ +(N)2 = (0, 1011001)2cns

◼ -(N)2 = [+(N)2]2 = [0, 1011001]2 = (1, 0100111)2cns

24 Chapter 1
Radix Complement Number Systems (5)
◼ Example: Two's complement number system representation
of -(18)10 , n = 8:
 +(18)10 = (0, 0010010)2cns
 -(18)10 = [0, 0010010]2 = (1, 1101110)2cns

◼ Example: Decimal representation of N = (1, 1101000)2cns


 N = (1, 1101000)2cns = -[1, 1101000]2 = -(0, 0011000)2cns
= -(24)2
Fixed point number

± 0 1 1 0 0 1 0 0 1

Sign Radix Fractional bits


Integer bits
point

Fixed point number: There is a fixed position separating the integer


and fractional parts.
Problem: can't decide how many bits for integer and fraction are
appropriated
Floating point number

± 0 1 1 1 0 0 1 1 1

Sign E m
bit (Mantissa)
(Exponent)
Floating Point Numbers (1)
◼ N = M ´ rE, where
 M (mantissa or significand) is a significant digits of N
 E (exponent or characteristic) is an integer exponent.

◼ In general, N = ± (an-1 ... a0 .a-1 ... a-m)r is represented by


 N = ± (.an-1 ... a-m)r ´ rn

◼ M is usually represented in sign magnitude:


 M = (SM.an-1 ... a-m)rsm , where ( −1) M  (. a n −1 ... a − m ) r
S

 (.an-1 ... a-m)r represents the magnitude


 SM = (0: positive, 1: negative)
Floating point number
IEEE 32bit

Số dấu chấm động có dạng:


SEM
B1: Chuyển đổi số trên ra hệ hai
-2345,125 = -1001 0010 1001.001

B2: Chuẩn hoá theo IEEE 32bit


-1001 0010 1001.001
=> -1.001 0010 1001 001 x 2^11

B3: Xác định các thông số biểu diễn S,E,M


S: phần định trị là số âm, nên S là 1
E : phần mũ được xác định là 8 bit E = 11+127=138=10001010
M: phần định trị được xác định là 001 0010 1001 0010 0000 0000 (23 bit)

Dãy số là:
SE M
1 10001010 00100101001001000000000
Coding
Assume that you want to communicate with
your friend with a flashlight in a night, what
will you do?

Drawing the word?


What is the problem?
Coding
Solution #1
•A: 1 blink
•B: 2 blinks
•C: 3 blinks
:
•Z: 26 blinks

What’s the problem?


•How are you? = 131 blinks
Coding
Solution #2 - Morse code

Hello
Lookup

•It is easy to translate into Morse


code than reverse. Why?
Tra mã hóa (Lookup)

Tại sao lại phải dùng . và -?

Số của mã = 2số chấm và số gạch


Tra mã hóa (Lookup)

Useful for
checking the
correctness/
redundency
Mã hóa (Lookup) – Braille

Louis Braille Braille code


Tra mã hóa (Lookup)

Braille
Tra mã hóa (Lookup)

Morse Braille

Both of these
encodings are binary
Binary representations
•Electronic Implementation
–Easy to store with bistable elements
–Reliably transmitted on noisy and inaccurate wires

0 1 0
3.3V
2.8V

0.5V
0.0V
Why encryption?

Today's digital computers only understand the


numbers 0 and 1, so any information in the
form of digits, letters, characters ... must be
converted to binary form
Coding areas

Coding areas include:


- Decimal encoding
- Character encoding
- Script encoding
- Voice coding
- Image encoding
- …
BCD – Binary coded

Bitcoin Code Diamond

4 bits are
required to
encode decimal
numbers
BCD – Binary coded decimal
Category:
• Weighted BCD code
• Non-weighted BCD code
(excess-3, gray, ASCII)
BCD – Binary coded decimal
• A weighted BCD code is one that allows analysis into
polynomials according to its weight.
• Weighted BCD codes are divided into two types of codes:
- Natural BCD Code: A code in which the weights are
usually arranged in ascending order
Example: BCD 8421, BCD 5421

- Arithmetic BCD code; A code that is symmetric through a


middle path
Example : BCD 2421, BCD 5121, BCD8 4-2-1
BCD – Binary coded decimal
◼ BCD is used to represent information such as strings of letters or
numbers
◼ Each position has a weight
◼ BCD Codes:
0: 0000 1: 0001 2: 0010 3: 0011 4: 0100
5: 0101 6: 0110 7: 0111 8: 1000 9: 1001
 Example: (9750)10 = (1001 0111 0101 0000)BCD
BCD – Binary coded decimal
BCD – Binary coded decimal
Arithmetic BCD code
Đối xứng nhau
BCD – Binary coded decimal
BCD – Binary coded decimal
BCD – Binary coded decimal
BCD 8421 code recognition circuit:

y = 1 Not BCD
y = 0 BCD

There will be no number


where a3 and a1 or a3 and
a2 are equal to 1
BCD – Binary coded decimal

Mạch nhận dang mã BCD 8421:


y = Not BCD
y = 0 BCD

There will be no number where a3 and a1


or a3 and a2 are equal to 1
Các phép tính trên số BCD
Do số BDC chỉ có giá trị từ 0->9 nên đối với những số thập phân lớn hơn sẽ
chia số thập phân thành các phần, mỗi phần được biểu diễn bởi 1 số BCD
tương ứng:
Các phép tính trên số BCD
Phép trừ tính trên số BCD theo quy tắc sau:

A – B = A + B’ trong đó B’ là phần bù của B


BCD advantages and disadvantages
ASCII
◼ ASCII (American Standard Code for Information Interchange)
 Character sets
Standard ASCII(0 – 127)
Extended ASCII (0 – 255)
◼ – ANSI (0 – 255)
◼ – Unicode (0 – 65,535)

Character BCD Decimal


D 1000100 44
i 1101001 69
g 1100111 67
i 1101001 69
t 1110100 74
a 1100001 61
l 1101100 6C
ASCII
Assignment
Bài tập 1:
Chuyển đổi các số thập phân sau sang hệ nhị phân, bat phân và thập lục phân:
9210, 14410, 40910, 25410, 25610, 6452210

Bài tập 2:
Chuyển đổi các số sau sang số bù nhị phân 8 bit: -110, -510, -1710, -6410, -12810,
2510.

Bài tập 3:
Thực hiện phép tính số học sau bằng phương pháp trừ nhị phân và sử dụng
số bù 2: 6110 - 1710 = ?

Bài tập 4:
Biểu diễn các số thập phân sau bằng mã nhị phân vầ mã BCD(2421): 1610 và
199910
Error Detection and
Correction code
Error detection/correction code: Information is encoded in such a
way that a particular class of errors can be detected and/or corrected.

5
8
Contents
• Concept
• Hamming code
• CRC check
Concepts
◼ An error: An incorrect value in one or more bits.
◼ Single error: An incorrect value in only one bit.
◼ Multiple error: One or more bits are incorrect.
◼ Errors are introduced by hardware failures, external interference (noise), or
other unwanted events.
Concepts
• Error detection rule: If only transmitting the correct
data to be transmitted, the error cannot be detected
Need to send more information with the data
 Information attached to help detect errors

• Method: Mã khối – Block coding


Split the data into k-bit blocks and convert to n-bit
blocks, where n = k +r

=> Error detection and correction is done at the Data link


layer
Concepts
• Example: Vertical Redundancy Check (VRC) technique
Add a bit to the end of the data block, according to the rule: "Total
number of 1's is even“
The extra bit is called Parity Check.

Example: check even


0110011 → 01100110 // bit thêm vào là 0 để tổng số bit 1 là chẵn

0110001 → 01100011 // bit thêm vào là 1 để tổng số bit 1 là chẵn

Comment: Only odd number of errors detected


62
Concepts
• Model

63
Model
Example:

Data word

0110011 Lỗi

Thêm bit check


Add kiểm tra
bit Check number of 1s

01100110 00100110
Code word

64
Method – Error detection
• To detect errors:
- The receiver has a list of correct code
words
- If the received code word is not in the
list -> there is an error
=> Comment: only detect errors according to the
preset design

65
Example:
Word code table for error detection
• Code word (code word) has an even number of 1’s
• Codewords do not overlap, differ by 2 bits

66
Hamming distance

• Khoảng cách Hamming giữa 2 từ cùng độ dài : là số bit


(ở cùng vị trí ) khác nhau
• Khoảng cách Hamming tối thiểu : là khoảng cách nhỏ
nhất giữa 2 cặp bất kỳ có trong một tập từ.
Ví du: d(000, 011) là 2 Vì

67
Khoảng cách Hamming
dmin trong bảng sau là 2

68
Khoảng cách Hamming

• Định lý 1 : Để đảm bảo phát hiện lỗi của s bit


thì khoảng cách Hamming cực tiểu trong các
cặp từ mã phải là dmin = s+1

• Định lý 2: để sửa t lỗi , khoảng cách Hamming


cực tiểu trong các từ mã phải là
dmin = 2t +1

69
Mã Hamming

• Dùng để sửa lỗi


• Mã Hamming xem xét trong bài này có
dmin = 3
=> Theo định lý 2: dùng để sửa 1 lỗi

70
Cấu trúc của bộ mã hóa và bộ giải mã Hamming C(7,4)

71
Mã Hamming
Cho bảng mã Hamming C(7,4)

72
Mã Hamming
Ký hiệu :
Data word : a3 a2 a1 a0 1010
Code word : a3 a2 a1 a0 r2 r1 r0 1010001

Xác định code word <=> tính r2 r1 r0


r0=a2+a1+a0 module 2
r1=a3+a2+a1 module 2
r2=a0+a1+a3 module 2

73
Giải Mã Hamming
Chuỗi bit nhận được cần kiểm tra : b3b2b1b0q2q1q0
(1)Tính syndrome: s2s1s0
(2)nếu s2s1s0 = 000 thì không có lỗi
ngược lại: =>có lỗi

s0=b2+b1+b0+q0 module 2 = 1 % 2 =1
s1=b3+b2+b1+q1 module 2 = 1 %2 =1
s2=b0+b1+b3+q2 module 2 =(0+0+1+0)%2 =1

74
Sửa Lỗi Mã Hamming
Cách sửa lỗi :
(1)tìm bit bị lỗi dựa vào bảng dò lỗi sau
(2)đảo ngược bit lỗi

Logical decision made by the correction logic analyzer

75
Mã Hamming
•VD1 : Bên nhận nhận được code word 0100011 Xác định dataword?
Giải :
0100011: b0 = 0/ b1 = 0/ b2 =1/ b3 =0/ q0=1/ q1=1/q2=0

s0=b2+b1+b0+q0
s0=1+0+0+1 = 10 module 2 = 0

s1=b3+b2+b1+q1
s1= 0+1+0+1 = 10 module 2 = 0

s2=b0+b1+b3+q2
s2=0+0+0+0 = 0 module 2 = (0+0+0+0)%2=0

Tính syndrome s2s1s0= 000


Kết luận : Code word nhận được không lỗi
Data word là 4 bit cực trái: 0100
76
Mã Hamming
•VD2 : Bên nhận nhận được code word
0101011 Xác định data word ?

Giải :
Tính syndrome s2s1s0 = 101
(ghi chi tiết cách tính s2, s1, s0 … )

Kết luận : code word nhận được bị lỗi


Dựa theo bảng : bit lỗi là b0 Sửa bit b0
=> Data word là 4 bit cực trái : 0100
Mã Hamming
Bài tập:
Xác định lỗi nếu có và sửa lỗi dựa trên tín hiệu
nhận được sử dụng mã Hamming

Codeword: 0101010, 1111011, 0111011


Mã phát hiện lỗi
Cyclic Redundancy Check (CRC)

• CRC là một phương pháp để phát hiện lỗi


bằng cách gắn thêm một khối bit phía sau
khối dữ liệu
• Các bit bổ sung thêm vào gọi là các bit CRC

• Có một số phương pháp cài đặt : modulo 2,


đa thức, …

79
Mô hình mã và giải mã CRC

80
Mô hình mã và giải mã CRC
• Bên gửi :
– Đầu vào : Dataword 4 bit
– Dùng bộ mã để tính r2r1r0
=> thu được Codeword 7 bit
• Bên nhận :
– Nhận Codeword 7 bit

– Dùng bộ giải mã để tính Syndrome


• Nếu Syndrome ≠ 0 => một hoặc nhiều bit bị lỗi
• Nếu Syndrome = 0 : 2 trường hợp
– Không có lỗi => Thu được Dataword
– Một số bit bị lỗi , nhưng bộ giải mã không phát hiện được

81
Mô hình mã và giải mã CRC
• Bên gửi : thực hiện mã Dataword k bits (k= 4) => kết
quả thu được Codeword n bits (n= 7) => truyền Codeword
đi.

Quy trình mã hóa với Dataword k =4bit, Codeword n =7:


(1)Thêm vào bên phải Dataword số bit 0 là n-k (3 bit)
(2)Một số chia xác định trước d3d2d1d0, có số bit n-k+1
Ví dụ : 1011
(3) Thực hiện phép chia modulo 2 giữa Dataword n bit với số
chia d3d2d1d0
(4) Phần dư của phép chia modulo 2 là số bit CRC
(5) Gắn số bit CRC vào bên phải Dataword: thu được Codewod

82
Bên gửi

83
Mô hình mã và giải mã CRC
• Bên nhận : Đưa Codeword vào bộ giải mã để tính
syndrome => Dựa trên giá trị của syndrome để phát
hiện có lỗi hay không

(1)Thực hiện phép chia modulo 2 giữa Codeword


nhận được với số chia d3d2d1d0 (vd : 1011)
=> thu được : Phần dư của phép chia modulo 2 là số
syndrome
(2)Xem xét giá trị của syndrome để phát hiện có lỗi
không : 2 trường hợp

84
Trường hợp 1 : Codeword nhận được là
codeword đúng
Bên nhận

85
Trường hợp 2 : Codeword nhận được là
codeword sai
Bên nhận

86
Tính mã CRC
◼ 1001
• 1011 ) 1 0 1 0 0 0 0
◼ 1011
◼ 0010
◼ 0000
◼ 0100
◼ 0000
◼ 1000
◼ 1011
◼ 0 1 1 (số dư )=> mã CRC cần tính
87
Test 1
• 1011 ) 1 0 1 0 0 1 1
1011
0010
0000
0101
0000
1011
1011
0 0 0 => codeword không lỗi

19/09/2016 88
Test 2
• 1011 ) 1 0 1 1 0 1 1
1011
0000
0000
0001
0000
0011
0000
0 1 1 => codeword lỗi

89
CRC
• Bài tập : Tìm mã CRC với C(7,4) của dữ liệu
gửi là 1010, 1001, 1110
(Số chia dung trong bộ mã và giải mã là 1011)

19/09/2016 90
CRC
• Bài tập : Kiểm tra mã codeword nhận được
sau là đúng hay sai.
• 1011010
• 1010011
• 1001001
• 0011010
Nếu đúng hãy tách lấy dataword
Mã sử dụng là mã CRC với C(7,4)
(Số chia dung trong bộ mã và giải mã là 1001)
19/09/2016 91
Tổng kết
1. Hệ số đếm và các phép toán
2. Mã hóa tín hiệu số
3. Mã hóa và sửa lỗi

You might also like