0% found this document useful (0 votes)
45 views33 pages

2b Codes

The document discusses various coding methods for representing numeric and alphanumeric information digitally, including binary, binary-coded decimal (BCD), Gray code, and ASCII codes. It also describes the parity method for detecting single-bit errors during data transmission by adding an extra parity bit to make the total number of 1 bits either even or odd.

Uploaded by

microwave python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views33 pages

2b Codes

The document discusses various coding methods for representing numeric and alphanumeric information digitally, including binary, binary-coded decimal (BCD), Gray code, and ASCII codes. It also describes the parity method for detecting single-bit errors during data transmission by adding an extra parity bit to make the total number of 1 bits either even or odd.

Uploaded by

microwave python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

2b.

Codes

• Students are required to do self-study for


this topic.
• Essential concepts will be discussed in
Tutorial 1.

2b-1
Quick links to each section

1. Encoding information
2. Straight Binary Coding
3. Binary-coded-decimal (BCD) code
4. Gray code
5. Alphanumeric code
6. Parity Method for Error Detection
7. Commonly used prefixes

2b-2
Encoding
Numbers, letters or words are represented by a special
group of symbols. A group of symbols is called a code.

For example, you may use the following binary code with
your friend:
00: let’s go eat lunch
01: let’s go play basketball
10: let’s go to the library
11: let’s study for tomorrow’s quiz

Both you and your friend must agree what each code word
means for it to work.

2b-3
Straight binary coding
In digital systems, numbers are probably the most common
type of information that need to be represented.
It is very common to represent a numerical value in binary,
i.e. base-2.
e.g. the decimal value 35 is simply represented as 100011 in
binary. This is called straight binary coding or simply binary
coding.
Note: 3510 = 2^5 + 2^1 + 2^0

There are other commonly used codes for representing


numbers.

2b-4
Binary-Coded-Decimal Code (BCD)

• Encode decimal numbers; combine some features


of decimal and binary systems
• Each digit of a decimal number is represented by
its 4-bit binary equivalent
• The legitimate digits are 0, 1, 2, 3, 4, 5, 6, 7, 8 and
9
• Since the largest decimal digit is 9, 4 bits are
required for each digit.

2b-5
Decimal digit BCD equivalent
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1

2b-6
Notice that the following bit patterns are illegal
in BCD code:
1010
1011
1100
1101
1110
1111

BCD code is used in digital machines whenever


decimal information is either applied as inputs or
displayed as outputs
2b-7
Representation in BCD
E.g. Represent decimal 957 in BCD

• Decimal 9 = 1001 in BCD


• Decimal 5 = 0101 in BCD
• Decimal 7 = 0111 in BCD
• Thus decimal 957 = 1001 0101 0111 in BCD
• Contrast this with
decimal 957 = 11 1011 1101 in straight
binary

2b-8
Characteristics of BCD
• Relative ease of conversion
• Consists of groups of 4-bit codes for decimal
digits 0-9
• Important from hardware standpoint – logic
circuits perform conversion to and from decimal
digits, all digits can be converted simultaneously
• E.g. converting 957 to binary requires repeated
division, but converting it to BCD does not.

2b-9
BCD code is not used in

• High speed computers


– it requires more bits than binary, and is
therefore less efficient
– E.g. decimal 3 in binary is 11, but decimal 3 in
BCD is 0011
– arithmetic processes represented in BCD
code are more complicated and slower

2b-10
Exercise

1. Convert 3410 to BCD

2. Convert 19.2510 to BCD

Answers:
0011 0100
0001 1001. 0010 0101

2b-11
Gray code

• Belongs to a class of codes called minimum-


change codes
• Only 1 bit in the code group changes when going
from 1 step to the next
• Unweighted code: Bit positions do not have any
specific weight (contrast with position-value
numbers)
• Usually cyclical: the last codeword and the first
codeword only has 1 bit difference

2b-12
Decimal 4-bit Gray code
0 0 0 0 0
1 0 0 0 1
2 0 0 1 1
3 0 0 1 0
4 0 1 1 0
5 0 1 1 1
6 0 1 0 1
Only 1 bit is
7 0 1 0 0 changed from
8 1 1 0 0 one value to the
9 1 1 0 1 next
10 1 1 1 1
11 1 1 1 0
12 1 0 1 0
13 1 0 1 1
14 1 0 0 1
15 1 0 0 0
2b-13
Example - Error occurring while using BCD
• what happens when a number increments from 1 to
2?

BCD code
Dec b3 b2 b1 b0
1 0 0 0 1

2 0 0 1 0

2b-14
Example - Error occurring while using BCD
• what happens when a number increments from 1 to
2?

Ideally, the bits b1 and b0 change at the same time:

Dec b3 b2 b1 b0
1 0 0 0 1

2 0 0 1 0

No error

2b-15
• possible actual case, bit b0 changes before b1:

Dec b3 b2 b1 b0
1 0 0 0 1

0 0 0 0 0

2 0 0 1 0

Transition error due to different speeds of bit


change – race condition

2b-16
Solution - Error occurring while using BCD
• Use Gray code instead and there will be no such
problem since only 1 bit (b1) is changed when the
number increments from 1 to 2

Gray code
Dec b3 b2 b1 b0
1 0 0 0 1

2 0 0 1 1

2b-17
Gray code is useful in situations where
multiple bit change may lead to error.

Gray code is not suitable for arithmetic


operations.

You may read section 2.11 of the textbook


by Wakerly for details.

2b-18
Alphanumeric Codes
• Codes that represent
– alphabet (e.g. a, b, c, …, z)
– punctuation marks
– special characters and numbers
• A complete set of alphanumeric code must include
– 26 lowercase letters (a – z)
– 26 uppercase letters (A – Z)
– 10 numeric digits (0 – 9)
– 7 punctuation marks
– 20 – 40 other characters such as +, -, /, <, #, %, …

2b-19
ASCII Code
• Most widely used alphanumeric code
• 7-bit code, hence 128 (=27) possible code symbols
• There is also the 8-bit extended ASCII code
• Used for transferring alphanumeric data between
digital devices
• Used in digital computers to store alphanumeric
characters

Students are NOT required


to memorize the ASCII table

2b-20
b7 is MSB, b1 is LSB
2b-21
Example:
Mp3 encoded into ASCII, will become

100110111100000110011

M p 3

Note: Lower case and upper case alphabets


have different codes

2b-22
Often times, hexadecimal digits are used to
represent ASCII codes.

Example:
Character ASCII expressed in Hex
M 0100 1101 4D
p 0111 0000 70
3 0011 0011 33

0 is padded to MSB before


converting to Hex

2b-23
Different ways to represent 1410

• Straight binary: 1110


• Hexadecimal: E or e
• Octal: 16
• BCD: 0001 0100
• ASCII: 0110001 0110100
• Gray code: 1001
• Note the importance of knowing which
representation is being used

2b-24
Parity Method for Error Detection

• Transfer of binary data from one location to


another can be corrupted by noise.

Actual signal

Received signal

2b-25
• Result of error:
transmitted 0 becomes 1 at the receiver
transmitted 1 becomes 0 at the receiver
• e.g. 1010 wrongly received as 1011, or
1100 wrongly received as 1000

• Multiple bit errors cannot be detected by this


simple parity method. They are much less likely
to happen than single bit errors.

2b-26
Parity Bit

• An extra bit attached to a code group


• It forms part of the code being transmitted
• The parity bit is made either 0 or 1
• Even parity make total no. of ‘1’ bits even BEFORE
transmitting

0 0 1 0 1 0 1 1

4 bits of 1 in data (4
Parity bit
is an even number)
2b-27
• Odd parity make total no. of ‘1’ bits odd BEFORE
transmitting

1 1 0 1 1 1 1
1
6 bits of 1 in data (7
Parity bit is an odd number)

• Is able to detect single bit error only

• Receiver and transmitter must agree on odd/


even parity scheme

2b-28
Examples:
7-bit data 8-bits are transmitted after
to be adding parity bit
transmitted
Odd parity Even parity
1001000 11001000 01001000
0011100 00011100 10011100
0101110 10101110 00101110
1010111 01010111 11010111

2b-29
Example: Limitation of parity method

• Transmitter and receiver agree on even parity


system
• Data to be transmitted: 1010111
• Data transmitted with parity bit: 11010111
• Actual data received corrupted by noise:
11000111 – one bit error
• Receiver checks parity: odd
• Receiver correctly concludes data in error
• If actual data received: 01010110 – two bit error
• Receiver checks parity: even
• Receiver wrongly concludes data no error!
2b-30
Commonly Used Prefixes

SI units IEC
• k (kilo) = 103 • Ki (kibi) = 210
• M (mega) = 106 • Mi (mebi) = 220
• G (giga) = 109
• Gi (gibi) = 230
JEDEC • Ti (tebi) = 240
• K (kilo) = 210
• M (mega) = 220
• G (giga) = 230 Binary prefix - Wikipedia, the free
encyclopedia
• T (tera) = 240

2b-31
Commonly Used Prefixes (cont)

Metric system
• m (milli) = 10-3
• µ (micro) = 10-6
• n (nano) = 10-9
• p (pico) = 10-12

Example:
• 0.1 µs = 100 ns = 10-7 second
• 200 mV = 0.2 V

2b-32
About significant figures

The value of pi is 3.1415926…


It can also be written as
• 3.14159 (6 significant figures)
• 3.1416 (5 sf)
• 3.142 (4 sf)
• 3.14 (3 sf)
• 3.1 (2 sf)
• 3 (1 sf)

2b-33

You might also like