0% found this document useful (0 votes)
44 views74 pages

Chapter 1.2 - Number Systems and Codes

Uploaded by

hoangvu2k4cmg
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)
44 views74 pages

Chapter 1.2 - Number Systems and Codes

Uploaded by

hoangvu2k4cmg
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/ 74

Number Systems and

Codes
© IT - TDT Number Systems and Codes 2

Acknowledgement
• The contents of these slides have origin from School of
Computing, National University of Singapore.
• We greatly appreciate support from Mr. Aaron Tan Tuck
Choy for kindly sharing these materials.
© IT - TDT Number Systems and Codes 3

Policies for students


• These contents are only used for students PERSONALLY.
• Students are NOT allowed to modify or deliver these
contents to anywhere or anyone for any purpose.
© IT - TDT Number Systems and Codes 4

Recording of modifications
• Delete slide QUICK REVIEW QUESTIONS
CS2100 Number Systems and Codes 5

NUMBER SYSTEMS & CODES


• Information Representations
• Number Systems
• Base Conversion
• Negative Numbers
• Excess Representation
• Floating-Point Numbers
• Decimal codes: BCD, Excess-3, 2421, 84-2-1
• Gray Code
• Alphanumeric Code
• Error Detection and Correction
CS2100 Number Systems and Codes 6

INFORMATION REPRESENTATION (1/3)


• Numbers are important to computers
• Represent information precisely
• Can be processed

• Examples
• Represent yes or no: use 0 and 1
• Represent the 4 seasons: 0, 1, 2 and 3

• Sometimes, other characters are used


• Matriculation number: 8 alphanumeric characters (eg:
U071234X)
CS2100 Number Systems and Codes 7

INFORMATION REPRESENTATION (2/3)


• Bit (Binary digit)
• 0 and 1
• Represent false and true in logic
• Represent the low and high states in electronic devices

• Other units
• Byte: 8 bits
• Nibble: 4 bits (seldom used)
• Word: Multiples of byte (eg: 1 byte, 2 bytes, 4 bytes, 8
bytes, etc.), depending on the architecture of the
computer system
CS2100 Number Systems and Codes 8

INFORMATION REPRESENTATION (3/3)


• N bits can represent up to 2N values.
• Examples:
• 2 bits à represent up to 4 values (00, 01, 10, 11)
• 3 bits à rep. up to 8 values (000, 001, 010, …, 110, 111)
• 4 bits à rep. up to 16 values (0000, 0001, 0010, …., 1111)

• To represent M values, élog2Mù bits are required.


• Examples:
• 32 values à requires 5 bits
• 64 values à requires 6 bits
• 1024 values à requires 10 bits
• 40 values à how many bits?
• 100 values à how many bits?
CS2100 Number Systems and Codes 9

DECIMAL (BASE 10) SYSTEM (1/2)


• A weighted-positional number system
• Base or radix is 10 (the base or radix of a number system is the
total number of symbols/digits allowed in the system)
• Symbols/digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
• Position is important, as the value of each symbol/digit is
dependent on its type and its position in the number
• Example, the 9 in the two numbers below has different values:
• (7594)10 = (7 × 103) + (5 × 102) + (9 × 101) + (4 × 100)
• (912)10 = (9 × 102) + (1 × 101) + (2 × 100)
• In general,
(anan-1… a0 . f1f2 … fm)10 =
(an x 10n) + (an-1x10n-1) + … + (a0 x 100) +
(f1 x 10-1) + (f2 x 10-2) + … + (fm x 10-m)
CS2100 Number Systems and Codes 10

DECIMAL (BASE 10) SYSTEM (2/2)


• Weighing factors (or weights) are in powers of 10:
… 103 102 101 100 . 10-1 10-2 10-3 …
• To evaluate the decimal number 593.68, the digit in each
position is multiplied by the corresponding weight:
5´102 + 9´101 + 3´100 + 6´10-1 + 8´10-2
= (593.68)10
CS2100 Number Systems and Codes 11

OTHER NUMBER SYSTEMS (1/2)


• Binary (base 2)
• Weights in powers of 2
• Binary digits (bits): 0, 1
• Octal (base 8)
• Weights in powers of 8
• Octal digits: 0, 1, 2, 3, 4, 5, 6, 7.

• Hexadecimal (base 16)


• Weights in powers of 16
• Hexadecimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.

• Base/radix R:
• Weights in powers of R
CS2100 Number Systems and Codes 12

OTHER NUMBER SYSTEMS (2/2)


• In some programming languages/software, special
notations are used to represent numbers in certain bases
• In programming language C
• Prefix 0 for octal. Eg: 032 represents the octal number (32)8
• Prefix 0x for hexadecimal. Eg: 0x32 represents the hexadecimal number
(32)16
• In PCSpim (a MIPS simulator)
• Prefix 0x for hexadecimal. Eg: 0x100 represents the hexadecimal
number (100)16
• In Verilog, the following values are the same
• 8’b11110000: an 8-bit binary value 11110000
• 8’hF0: an 8-bit binary value represented in hexadecimal F0
• 8’d240: an 8-bit binary value represented in decimal 240
CS2100 Number Systems and Codes 13

BASE-R TO DECIMAL CONVERSION


• Easy!
• 1101.1012 = 1´23 + 1´22 + 1´20 + 1´2-1 + 1´2-3

• 572.68 =

• 2A.816 =

• 341.245 =

±
CS2100 Number Systems and Codes 14

DECIMAL TO BINARY CONVERSION


• Method 1
• Sum-of-Weights Method

• Method 2
• Repeated Division-by-2 Method (for whole numbers)
• Repeated Multiplication-by-2 Method (for fractions)
CS2100 Number Systems and Codes 15

SUM-OF-WEIGHTS METHOD
• Determine the set of binary weights whose sum is
equal to the decimal number
• (9)10 = 8 + 1 = 23 + 20 = (1001)2

• (18)10 = 16 + 2 = 24 + 21 = (10010)2

• (58)10 = 32 + 16 + 8 + 2 = 25 + 24 + 23 + 21 = (111010)2

• (0.625)10 = 0.5 + 0.125 = 2-1 + 2-3 = (0.101)2


CS2100 Number Systems and Codes 16

REPEATED DIVISION-BY-2
§ To convert a whole number to binary, use successive
division by 2 until the quotient is 0. The remainders form
the answer, with the first remainder as the least
significant bit (LSB) and the last as the most significant
bit (MSB).
(43)10 = (101011)2 2 43
2 21 rem 1 ß LSB
2 10 rem 1
2 5 rem 0
2 2 rem 1
2 1 rem 0
0 rem 1 ß MSB
CS2100 Number Systems and Codes 17

REPEATED MULTIPLICATION-BY-2
§ To convert decimal fractions to binary, repeated
multiplication by 2 is used, until the fractional product is 0
(or until the desired number of decimal places). The
carried digits, or carries, produce the answer, with the
first carry as the MSB, and the last as the LSB.
(0.3125)10 = (.0101)2
Carry
0.3125´2=0.625 0 ßMSB
0.625´2=1.25 1
0.25´2=0.50 0
0.5´2=1.00 1 ßLSB
CS2100 Number Systems and Codes 18

CONVERSION BETWEEN
DECIMAL AND OTHER BASES
§ Base-R to decimal: multiply digits with their
corresponding weights.
§ Decimal to binary (base 2)
§ Whole numbers repeated division-by-2
§ Fractions: repeated multiplication-by-2
§ Decimal to base-R
§ Whole numbers: repeated division-by-R
§ Fractions: repeated multiplication-by-R
CS2100 Number Systems and Codes 19

CONVERSION BETWEEN BASES


• In general, conversion between bases can be
done via decimal:
Base-2 Base-2
Base-3 Base-3
Base-4 Decimal Base-4
… ….
Base-R Base-R

n Shortcuts for conversion between bases 2, 4, 8,


16 (see next slide)
CS2100 Number Systems and Codes 20

BINARY TO OCTAL/HEXADECIMAL
CONVERSION
• Binary à Octal: partition in groups of 3
• (10 111 011 001 . 101 110)2 =

• Octal à Binary: reverse


• (2731.56)8 =

• Binary à Hexadecimal: partition in groups of 4


• (101 1101 1001 . 1011 1000)2 =

• Hexadecimal à Binary: reverse


• (5D9.B8)16 =

±
CS2100 Number Systems and Codes 21

PEEKING AHEAD (1/2)


• Function simplification (eg: Quine-McCluskey)

• In ‘computer-speak’, units are in powers of 2

• Memory addressing (see next slide)

±
CS2100 Number Systems and Codes 22

PEEKING AHEAD (2/2)


• Memory addressing
• Assume 210 bytes in memory, and each word contains 4 bytes.

Addresses
binary decimal
Memory
0000000000 0 00101101
0000000001 1 01010101
0000000010 2 10111100
0000000011 3 01111001
0000000100 4 11001100
0000000101 5 10000101
0000000110 6 11010111
0000000111 7 00011000
0000001000 8 01101101
0000001001 9 10011011
0000001010 10 11010101
0000001011 11 01000001
. . ˜
˜
. .
˜
1111111111 1023

±
CS2100 Number Systems and Codes 23

NEGATIVE NUMBERS
• Unsigned numbers: only non-negative values.
• Signed numbers: include all values (positive and
negative)
• There are 3 common representations for signed
binary numbers:
• Sign-and-Magnitude
• 1s Complement
• 2s Complement
CS2100 Number Systems and Codes 24

SIGN-AND-MAGNITUDE (1/3)
• The sign is represented by a ‘sign bit’
• 0 for +
• 1 for -

• Eg: a 1-bit sign and 7-bit magnitude format.

sign magnitude

q 00110100 è +1101002 = ?
q 10010011 è -100112 = ?

±
CS2100 Number Systems and Codes 25

SIGN-AND-MAGNITUDE (2/3)
• Largest value: 01111111 = +12710
• Smallest value: 11111111 = -12710
• Zeros: 00000000 = +010
10000000 = -010
• Range: -12710 to +12710
• Question:
• For an n-bit sign-and-magnitude representation, what is
the range of values that can be represented?
CS2100 Number Systems and Codes 26

SIGN-AND-MAGNITUDE (3/3)
• To negate a number, just invert the sign bit.
• Examples:
• How to negate 00100001sm (decimal 33)?
Answer: 10100001sm (decimal -33)
• How to negate 10000101sm (decimal -5)?
Answer: 00000101sm (decimal +5)
CS2100 Number Systems and Codes 27

1s COMPLEMENT (1/3)
• Given a number x which can be expressed as an n-bit
binary number, its negated value can be obtained in 1s-
complement representation using:
-x = 2n – x – 1
• Example: With an 8-bit number 00001100 (or 1210), its
negated value expressed in 1s-complement is:
-000011002 = 28 – 12 – 1 (calculation in decimal)
= 243
= 111100111s
(This means that -1210 is written as 11110011 in 1s-
complement representation.)
CS2100 Number Systems and Codes 28

1s COMPLEMENT (2/3)
• Essential technique to negate a value: invert all
the bits.
• Largest value: 01111111 = +12710
• Smallest value: 10000000 = -12710
• Zeros: 00000000 = +010
11111111 = -010
• Range: -12710 to +12710
• The most significant (left-most) bit still represents
the sign: 0 for positive; 1 for negative.
CS2100 Number Systems and Codes 29

1s COMPLEMENT (3/3)
• Examples (assuming 8-bit numbers):

(14)10 = (00001110)2 = (00001110)1s


-(14)10 = -(00001110)2 = (11110001)1s
-(80)10 = -( ? )2 = ( ? )1s
CS2100 Number Systems and Codes 30

2s COMPLEMENT (1/3)
• Given a number x which can be expressed as an n-bit
binary number, its negated value can be obtained in 2s-
complement representation using:
-x = 2n – x
• Example: With an 8-bit number 00001100 (or 1210), its
negated value expressed in 2s-complement is:
-000011002 = 28 – 12 (calculation in decimal)
= 244
= 111101002s
(This means that -1210 is written as 11110100 in 2s-
complement representation.)
CS2100 Number Systems and Codes 31

2s COMPLEMENT (2/3)
• Essential technique to negate a value: invert all
the bits, then add 1.
• Largest value: 01111111 = +12710
• Smallest value: 10000000 = -12810
• Zero: 00000000 = +010
• Range: -12810 to +12710
• The most significant (left-most) bit still represents
the sign: 0 for positive; 1 for negative.
CS2100 Number Systems and Codes 32

2s COMPLEMENT (3/3)
• Examples (assuming 8-bit numbers):

(14)10 = (00001110)2 = (00001110)2s


-(14)10 = -(00001110)2 = (11110010)2s
-(80)10 = -( ? )2 = ( ? )2s

Compare with slide 30.


n 1s complement:
(14)10 = (00001110)2 = (00001110)1s
-(14)10 = -(00001110)2 = (11110001)1s
CS2100 Number Systems and Codes 33

READING ASSIGNMENT
• Download from the course website and read the
Supplement Notes on Lecture 2: Number
Systems.
• Work out the exercises in there and discuss them
in the IVLE forum if you have doubts.
CS2100 Number Systems and Codes 34

COMPARISONS
4-bit system
Positive values Negative values

Value Sign-and- 1s 2s Value Sign-and- 1s 2s


Magnitude Comp. Comp. Magnitude Comp. Comp.
+7 0111 0111 0111 -0 1000 1111 -
+6 0110 0110 0110 -1 1001 1110 1111
+5 0101 0101 0101 -2 1010 1101 1110
+4 0100 0100 0100 -3 1011 1100 1101
+3 0011 0011 0011 -4 1100 1011 1100
+2 0010 0010 0010 -5 1101 1010 1011
+1 0001 0001 0001 -6 1110 1001 1010
+0 0000 0000 0000 -7 1111 1000 1001
-8 - - 1000
CS2100 Number Systems and Codes 35

COMPLEMENT ON FRACTIONS
• We can extend the idea of complement on
fractions.
• Examples:
• Negate 0101.01 in 1s-complement
Answer: 1010.10
• Negate 111000.101 in 1s-complement
Answer: 000111.010
• Negate 0101.01 in 2s-complement
Answer: 1010.11
CS2100 Number Systems and Codes 36

2s COMPLEMENT
ADDITION/SUBTRACTION (1/3)
• Algorithm for addition, A + B:
1. Perform binary addition on the two numbers.
2. Ignore the carry out of the MSB.
3. Check for overflow. Overflow occurs if the ‘carry in’ and ‘carry
out’ of the MSB are different, or if result is opposite sign of A and
B.
• Algorithm for subtraction, A – B:
A – B = A + (-B)
1. Take 2s-complement of B.
2. Add the 2s-complement of B to A.
CS2100 Number Systems and Codes 37

OVERFLOW
• Signed numbers are of a fixed range.
• If the result of addition/subtraction goes beyond this
range, an overflow occurs.
• Overflow can be easily detected:
• positive add positive à negative
• negative add negative à positive
• Example: 4-bit 2s-complement system
• Range of value: -810 to 710

• 01012s + 01102s = 10112s


510 + 610 = -510 ?! (overflow!)
• 10012s + 11012s = 101102s (discard end-carry) = 01102s
-710 + -310 = 610 ?! (overflow!)
CS2100 Number Systems and Codes 38

2s COMPLEMENT
ADDITION/SUBTRACTION (2/3)
• Examples: 4-bit system
+3 0011 -2 1110
+ +4 + 0100 + -6 + 1010
---- ------- ---- -------
+7 0111 -8 11000
---- ------- ---- -------
+6 0110 +4 0100
+ -3 + 1101 + -7 + 1001
---- ------- ---- -------
+3 10011 -3 1101
---- ------- ---- -------

n Which of the above is/are overflow(s)?

±
CS2100 Number Systems and Codes 39

2s COMPLEMENT
ADDITION/SUBTRACTION (3/3)
• Examples: 4-bit system

-3 1101 +5 0101
+ -6 + 1010 + +6 + 0110
---- ------- ---- -------
-9 10111 +11 1011
---- ------- ---- -------

n Which of the above is/are overflow(s)?

±
CS2100 Number Systems and Codes 40

1s COMPLEMENT
ADDITION/SUBTRACTION (1/2)
• Algorithm for addition, A + B:
1. Perform binary addition on the two numbers.
2. If there is a carry out of the MSB, add 1 to the result.
3. Check for overflow. Overflow occurs if result is opposite sign of
A and B.
• Algorithm for subtraction, A – B:
A – B = A + (-B)
1. Take 1s-complement of B.
2. Add the 1s-complement of B to A.
CS2100 Number Systems and Codes 41

1s COMPLEMENT
ADDITION/SUBTRACTION (2/2)
• Examples: 4-bit system Any overflow?
+3 0011 +5 0101
+ +4 + 0100 + -5 + 1010
---- ------- ---- -------
+7 0111 -0 1111
---- ------- ---- -------

-2 1101 -3 1100
+ -5 + 1010 + -7 + 1000
---- ------ ---- -------
-7 10111 -10 10100
---- + 1 ---- + 1
------ -------
1000 0101
CS2100 Number Systems and Codes 42

QUICK REVIEW QUESTIONS (4)


• DLD pages 42 - 43
Questions 2-13 to 2-18.
CS2100 Number Systems and Codes 43

EXCESS REPRESENTATION (1/2)


• Besides sign-and-magnitude and
complement schemes, the excess Excess-4
Value
representation is another scheme. Representation

• It allows the range of values to be 000 -4


distributed evenly between the
positive and negative values, by a 001 -3
simple translation 010 -2
(addition/subtraction).
011 -1
• Example: Excess-4 representation on
3-bit numbers. See table on the right. 100 0

101 1

110 2
n Questions: What if we use Excess-2 111 3
on 3-bit numbers? Excess-7?
CS2100 Number Systems and Codes 44

EXCESS REPRESENTATION (2/2)


• Example: For 4-bit numbers, we may use excess-7 or
excess-8. Excess-8 is shown below. Fill in the values.
Excess-8 Excess-8
Representation Value Representation Value

0000 -8 1000
0001 1001
0010 1010
0011 1011
0100 1100
0101 1101
0110 1110
0111 1111

±
CS2100 Number Systems and Codes 45

FIXED POINT NUMBERS (1/2)


• In fixed point representation, the binary point is
assumed to be at a fixed location.
• For example, if the binary point is at the end of an 8-bit
representation as shown below, it can represent
integers from -128 to +127.

binary point
CS2100 Number Systems and Codes 46

FIXED POINT NUMBERS (2/2)


• In general, the binary point may be assumed to
be at any pre-fixed location.
• Example: Two fractional bits are assumed as shown
below.

integer part fraction part


assumed binary point
q If 2s complement is used, we can represent values
like:
011010.112s = 26.7510
111110.112s = -000001.012 = -1.2510
CS2100 Number Systems and Codes 47

FLOATING POINT NUMBERS (1/4)


• Fixed point numbers have limited range.
• Floating point numbers allow us to represent very
large or very small numbers.
• Examples:
0.23 × 1023 (very large positive number)
0.5 × 10-37 (very small positive number)
-0.2397 × 10-18 (very small negative number)
CS2100 Number Systems and Codes 48

FLOATING POINT NUMBERS (2/4)


• 3 parts: sign, mantissa and exponent
• The base (radix) is assumed to be 2.
• Sign bit: 0 for positive, 1 for negative.

sign mantissa exponent

n Mantissa is usually in normalised form (the integer part


is zero and the fraction part must not begin with zero)
0.01101 × 24 à normalised à
101011.0110 × 2-4 à normalised à
n Trade-off:
q More bits in mantissa à better precision
q More bits in exponent à larger range of values

±
CS2100 Number Systems and Codes 49

FLOATING POINT NUMBERS (3/4)


• Exponent is usually expressed in complement or excess
format.
• Example: Express -6.510 in base-2 normalised form
-6.510 = -110.12 = -0.11012 × 23
• Assuming that the floating-point representation contains
1-bit, 5-bit normalised mantissa, and 4-bit exponent. The
above example will be stored as if the exponent is in 1s or
2s complement.

1 11010 0011
CS2100 Number Systems and Codes 50

FLOATING POINT NUMBERS (4/4)


• Example: Express 0.187510 in base-2 normalised form
0.187510 = 0.00112 = 0.11 × 2-2
• Assume this floating-point representation:1-bit sign, 5-bit
normalised mantissa, and 4-bit exponent.
• The above example will be represented as

0 11000 1101 If exponent is in 1’s complement.

0 11000 1110 If exponent is in 2’s complement.

0 11000 0110 If exponent is in excess-8.


CS2100 Number Systems and Codes 51

DECIMAL CODES
• Decimal numbers are favoured by humans. Binary
numbers are natural to computers. Hence, conversion is
required.
• If little calculation is required, we can use some coding
schemes to store decimal numbers, for data transmission
purposes.
• Examples: BCD (or 8421), Excess-3, 84-2-1, 2421, etc.
• Each decimal digit is represented as a 4-bit code.
• The number of digits in a code is also called the length of
the code.
CS2100 Number Systems and Codes 52

BINARY CODE DECIMAL (BCD) (1/2)


Decimal
• Some codes are unused, like BCD
digit
1010BCD, 1011BCD, … 1111BCD.
0 0000
These codes are considered
1 0001
as errors.
2 0010
• Easy to convert, but arithmetic 3 0011
operations are more 4 0100
complicated.
5 0101
• Suitable for interfaces such as 6 0110
keypad inputs. 7 0111
8 1000
9 1001
CS2100 Number Systems and Codes 53

BINARY CODE DECIMAL (BCD) (2/2)


• Examples of conversion between BCD values
and decimal values:
• (234)10 = (0010 0011 0100)BCD
• (7093)10 = (0111 0000 1001 0011)BCD
• (1000 0110)BCD = (86)10
• (1001 0100 0111 0010)BCD = (9472)10

• Note that BCD is not equivalent to binary


• Example: (234)10 = (11101010)2
CS2100 Number Systems and Codes 54

OTHER DECIMAL CODES


Decimal Digit BCD Excess-3 84-2-1 2*421 Biquinary
8421 5043210
0 0000 0011 0000 0000 0100001
1 0001 0100 0111 0001 0100010
2 0010 0101 0110 0010 0100100
3 0011 0110 0101 0011 0101000
4 0100 0111 0100 0100 0110000
5 0101 1000 1011 1011 1000001
6 0110 1001 1010 1100 1000010
7 0111 1010 1001 1101 1000100
8 1000 1011 1000 1110 1001000
9 1001 1100 1111 1111 1010000

• Self-complementing code: codes for complementary


digits are also complementary to each other.
• Error-detecting code: biquinary code (bi=two,
quinary=five).
CS2100 Number Systems and Codes 55

SELF-COMPLEMENTING CODES
• The codes representing the pair of complementary digits
are also complementary to each other.
• Example: Excess-3 code 0: 0011
1: 0100
2: 0101
3: 0110
4: 0111
5: 1000
6: 1001
7: 1010
8: 1011
9: 1100
n Question: What are the other self-complementing
codes?
±
CS2100 Number Systems and Codes 56

GRAY CODE (1/3)


• Unweighted (not an arithmetic code)
• Only a single bit change from one code value to the next.
• Not restricted to decimal digits: n bits à 2n values.
• Good for error detection.
• Example: 4-bit standard Gray code
Decimal Binary Gray Code Decimal Binary Gray code
0 0000 0000 8 1000 1100
1 0001 0001 9 1001 1101
2 0010 0011 10 1010 1111
3 0011 0010 11 1011 1110
4 0100 0110 12 1100 1010
5 0101 0111 13 1101 1011
6 0110 0101 14 1110 1001
7 0111 0100 15 1111 1000
CS2100 Number Systems and Codes 57

GRAY CODE (2/3)


• Generating a 4-bit standard Gray code sequence.

0 0 0 0 1
0 1 0 0
0 0 0 1 0
1 1 0 1
0 0 01 1 0
1 1 1 1
0 0 01 0 0
1 1 1 0
0 0
1 1 0 0
1 0 1 0
0 0
1 1 1 0
1 0 1 1
0 0
1 0 1 0
1 0 0 1
0 0
1 0 0 0
1 0 0 0

n Questions: How to generate 5-bit standard Gray code


sequence? 6-bit standard Gray code sequence?
CS2100 Number Systems and Codes 58

GRAY CODE (3/3)

1
1
sensors

0
0
0

0
1
0

0
0
1 0
1
0
1
0

1
1
0

0
1

1
mis-aligned

111 00 0
000 00 1
11 01

00 00

0 0
0 1

mis-aligned
0

0
0

1
sensors
000 0

001 0
0 sensors 1
010 10 010 11
0 0
1 0
000 11 11 010 000 00 11 111
1 1
1
11 11 1 1 11 1
0 0
11 1 00 0 1
00 01 1 11 11 1
0 0
111 0

110 1
11 0 1

000 1 0
0

11 00
11 01

1
00 1

11 0

0
0
0

Binary coded: 111 ® 110 ® 000 1


Gray coded: 111 ® 101
CS2100 Number Systems and Codes 59

ALPHANUMERIC CODES (1/3)


• Computers also handle textual data.
• Character set frequently used:
alphabets: ‘A’ … ‘Z’, ‘a’ … ‘z’
digits: ‘0’ … ‘9’
special symbols: ‘$’, ‘.’, ‘@’, ‘*’, etc.
non-printable: NULL, BELL, CR, etc.
• Examples
• ASCII (8 bits), Unicode
CS2100 Number Systems and Codes 60

ALPHANUMERIC CODES (2/3)


• ASCII
Character ASCII Code
• American Standard 0 0110000
Code for Information 1 0110001
... ...
Interchange 9 0111001
• 7 bits, plus a parity bit for : 0111010
A 1000001
error detection B 1000010
• Odd or even parity ... ...
Z 1011010
[ 1011011
\ 1011100
CS2100 Number Systems and Codes 61

ALPHANUMERIC CODES (3/3)


• ASCII table A: 1000001

MSBs
LSBs 000 001 010 011 100 101 110 111
0000 NUL DLE SP 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 “ 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EOT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ‘ 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y i y
1010 LF SUB * : J Z j z
1011 VT ESC + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 O RS . > N ^ n ~
1111 SI US / ? O _ o DEL
CS2100 Number Systems and Codes 62

ERROR
n
DETECTION (1/4)
Errors can occur during data transmission. They should
be detected, so that re-transmission can be requested.
n With binary numbers, usually single-bit errors occur.
q Example: 0010 erroneously transmitted as 0011 or 0000 or 0110
or 1010.
Decimal Biquinary
digit 5043210
0 0100001
n Biquinary code has
1 0100010
length 7; it uses 3 2 0100100
additional bits for 3 0101000
error-detection. 4 0110000
5 1000001
6 1000010
7 1000100
8 1001000
9 1010000
CS2100 Number Systems and Codes 63

ERROR DETECTION (2/4)


• Parity bit
• Even parity: additional bit
added to make total
number of 1’s even. Character ASCII Code
0 0110000 1
• Odd parity: additional bit
1 0110001 0
added to make total ... ...
Parity bits
number of 1’s odd. 9 0111001 1
: 0111010 1
A 1000001 1
• Example of odd parity B 1000010 1
on ASCII values. ... ...
Z 1011010 1
[ 1011011 0
\ 1011100 1
CS2100 Number Systems and Codes 64

ERROR DETECTION (3/4)


• Parity bit can detect odd
number of errors but not even
number of errors.
• Example: Assume odd parity,
• 10011 à 10001 (detected)
• 10011 à 10101 (not detected)
0110 1
0001 0
• Parity bits can also be applied 1011 0
to a block of data. 1111 1
1001 1
0101 0 Column-wise parity

Row-wise parity
CS2100 Number Systems and Codes 65

ERROR DETECTION (4/4)


• Sometimes, it is not enough to do error detection.
We may want to correct the errors.
• Error correction is expensive. In practice, we may
use only single-bit error correction.
• Popular technique: Hamming code
CS2100 Number Systems and Codes 66

ERROR CORRECTION (1/7)


• Given this 3-bit code C1
{ 000, 110, 011, 101 }
• With 4 code words, we actually need only 2 bits.
• We call this k, the number of original message bits.
• To add error detection/correction ability, we use
more bits than necessary.
• In this case, the length of each codeword is 3
• We define code efficiency (or rate) by
k / length of codeword
• Hence, efficiency of C1 is 2/3.
CS2100 Number Systems and Codes 67

ERROR CORRECTION (2/7)


• Given this 3-bit code C1
{ 000, 110, 011, 101 }
• Can C1 detect a single bit error?
• Can C1 correct a single bit error?

Sometimes, we use “1 error” for “single bit error”,


“2 errors” for “2 bits error”, etc.

±
CS2100 Number Systems and Codes 68

ERROR CORRECTION (3/7)


• The distance d between any two code words in a
code is the sum of the number of differences
between the codewords.
• Example: d(000, 110) = 2; d(0110,1011) = 3.
• The Hamming distance d of a code is the
minimum distance between any two code words
in the code.
• Example: The Hamming distance of C1 is 2.
• A code with Hamming distance of 2 can detect 1
error.
CS2100 Number Systems and Codes 69

ERROR CORRECTION (4/7)


• Given this 6-bit code C2
{ 000000, 111000, 001110, 110011 }
• What is its efficiency?
• What is its Hamming distance?
• Can it correct 1 error?

n Can it correct 2 errors?

±
CS2100 Number Systems and Codes 70

Self-study
ERROR CORRECTION (5/7)
• Hamming code: a popular error-correction code
• Procedure
• Parity bits are at positions that are powers of 2 (i.e. 1, 2,
4, 8, 16, …)
• All other positions are data bits
• Each parity bit checks some of the data bits
• Position 1: Check 1 bit, skip 1 bit (1, 3, 5, 7, 9, 11, …)
• Position 2: Check 2 bits, skip 2 bits (2, 3, 6, 7, 10, 11, …)
• Position 4: Check 4 bits, skip 4 bits (4-7, 12-15, 20-23, …)
• Position 8: Check 8 bits, skip 8 bits (8-15, 24-31, 40-47, …)
• Set the parity bit accordingly so that total number of 1s
in the positions it checks is even.
CS2100 Number Systems and Codes 71

Self-study
ERROR CORRECTION (6/7)
• Example: Data 10011010
• Insert positions for parity bits:
__1_001_1010
• Position 1: ? _ 1 _ 0 0 1 _ 1 0 1 0 à so ? must be 0
• Position 2: 0 ? 1 _ 0 0 1 _ 1 0 1 0 à so ? must be 1
• Position 4: 0 1 1 ? 0 0 1 _ 1 0 1 0 à so ? must be 1
• Position 8: 0 1 1 1 0 0 1 ? 1 0 1 0 à so ? must be 0

Answer: 0 1 1 1 0 0 1 0 1 0 1 0
CS2100 Number Systems and Codes 72

Self-study
ERROR CORRECTION (7/7)
• Suppose 1 error occurred and the received data
is:
011100101110
• How to determine which bit is in error?
• Check which parity bits are in error.
• Answer: parity bits 2 and 8.

• Add the positions of these erroneous parity bits


• Answer: 2 + 8 = 10. Hence data bit 10 is in error.

Corrected data: 0 1 1 1 0 0 1 0 1 0 1 0
CS2100 Number Systems and Codes 73

READING ASSIGNMENT
• Conversion between standard Gray code and
binary
• DLD page 1-24.
© IT - TDT Number Systems and Codes 74

Q&A

You might also like