Chapter 1.2 - Number Systems and Codes
Chapter 1.2 - Number Systems and Codes
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
Recording of modifications
• Delete slide QUICK REVIEW QUESTIONS
CS2100 Number Systems and Codes 5
• Examples
• Represent yes or no: use 0 and 1
• Represent the 4 seasons: 0, 1, 2 and 3
• 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
• Base/radix R:
• Weights in powers of R
CS2100 Number Systems and Codes 12
• 572.68 =
• 2A.816 =
• 341.245 =
±
CS2100 Number Systems and Codes 14
• 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
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
BINARY TO OCTAL/HEXADECIMAL
CONVERSION
• Binary à Octal: partition in groups of 3
• (10 111 011 001 . 101 110)2 =
±
CS2100 Number Systems and Codes 21
±
CS2100 Number Systems and Codes 22
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 -
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):
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):
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
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
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
---- ------- ---- -------
±
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
---- ------- ---- -------
±
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
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
0000 -8 1000
0001 1001
0010 1010
0011 1011
0100 1100
0101 1101
0110 1110
0111 1111
±
CS2100 Number Systems and Codes 45
binary point
CS2100 Number Systems and Codes 46
±
CS2100 Number Systems and Codes 49
1 11010 0011
CS2100 Number Systems and Codes 50
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
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
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
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
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
Row-wise parity
CS2100 Number Systems and Codes 65
±
CS2100 Number Systems and Codes 68
±
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.
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