Number Systems and Codes
Number Systems and Codes
Organisation
http://www.comp.nus.edu.sg/~cs2100
/
Number Systems and Codes
(AY2013/4) Semester 2
Number Systems
Read up DLD for details!
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
INFORMATION REPRESENTATION
(1/3)
Examples
CS2100
INFORMATION REPRESENTATION
(2/3)
0 and 1
Represent false and true in logic
Represent the low and high states in electronic
devices
Other units
CS2100
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
INFORMATION REPRESENTATION
(3/3)
Examples:
Examples:
CS2100
CS2100
In general,
CS2100
Octal (base 8)
Weights in powers of 8
Octal digits: 0, 1, 2, 3, 4, 5, 6, 7.
Weights in powers of 2
Binary digits (bits): 0, 1
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:
CS2100
Weights in powers of R
In programming language C
Prefix 0 for octal. Eg: 032 represents the octal number (32)8
CS2100
BASE-R TO DECIMAL
CONVERSION
Easy!
572.68 =
2A.816 =
341.245
CS2100
10
QUICK REVIEW
QUESTIONS (1)
DLD page 42
Questions 2-1 to 2-4.
CS2100
11
DECIMAL TO BINARY
CONVERSION
Method 1
Sum-of-Weights Method
Method 2
CS2100
12
SUM-OF-WEIGHTS METHOD
(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
CS2100
13
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
CS2100
2
2
2
2
2
2
43
21
10
5
2
1
0
rem 1 LSB
rem 1
rem 0
rem 1
rem 0
rem 1 MSB
14
REPEATED MULTIPLICATION-BY2
To convert decimal fractions to binary, repeated
CS2100
Carry
0
1
0
1
15
MSB
LSB
CONVERSION BETWEEN
DECIMAL AND OTHER BASES
Base-R to decimal: multiply digits with their
corresponding weights.
Decimal to base-R
Whole numbers: repeated division-by-R
Fractions: repeated multiplication-by-R
CS2100
16
QUICK REVIEW
QUESTIONS (2)
DLD page 42
Questions 2-5 to 2-8.
CS2100
17
CONVERSION BETWEEN
BASES
Base-R
Base-2
Base-3
Decimal
Base-4
.
Base-R
CS2100
18
BINARY TO OCTAL/HEXADECIMAL
CONVERSION
(2731.56)8 =
CS2100
(5D9.B8)16 =
Number Systems and Codes
19
QUICK REVIEW
QUESTIONS (3)
DLD page 42
Questions 2-9 to 2-10.
CS2100
20
READING ASSIGNMENT
CS2100
21
CS2100
22
Memory addressing
Memory
binary
decimal
00101101
0000000000
0
01010101
0000000001
1
10111100
0000000010
2
01111001
0000000011
3
11001100
0000000100
4
10000101
0000000101
5
11010111
0000000110
6
00011000
0000000111
7
01101101
0000001000
8
10011011
0000001001
9
11010101
0000001010
10
01000001
0000001011
11
.
.
.
.
1111111111
1023
CS2100
23
NEGATIVE NUMBERS
CS2100
Sign-and-Magnitude
1s Complement
2s Complement
24
SIGN-AND-MAGNITUDE
(1/3)
The sign is represented by a sign bit
0 for +
1 for -
magnitude
sign
00110100 +1101002 = ?
10010011 -100112 = ?
CS2100
25
SIGN-AND-MAGNITUDE
(2/3)
Largest value:
01111111 = +127
10
Zeros:
Question:
CS2100
00000000 = +010
10000000 = -010
26
SIGN-AND-MAGNITUDE
(3/3)
To negate a number, just invert the sign bit.
Examples:
CS2100
27
1s COMPLEMENT (1/3)
CS2100
28
1s COMPLEMENT (2/3)
Zeros:
CS2100
00000000 = +010
11111111 = -010
29
1s COMPLEMENT (3/3)
CS2100
30
2s COMPLEMENT (1/3)
CS2100
31
2s COMPLEMENT (2/3)
Zero:
CS2100
00000000 = +010
32
2s COMPLEMENT (3/3)
CS2100
1s complement:
(14)10 = (00001110)2 = (00001110)1s
-(14)10 = -(00001110)2 = (11110001)1s
Number Systems and Codes
33
READING ASSIGNMENT
CS2100
34
COMPARISONS
Important!
4-bit system
Positive values
Negative values
Value
Sign-andMagnitude
1s
Comp.
2s
Comp.
Value
Sign-andMagnitude
1s
Comp.
2s
Comp.
+7
+6
+5
+4
+3
+2
+1
+0
0111
0110
0101
0100
0011
0010
0001
0000
0111
0110
0101
0100
0011
0010
0001
0000
0111
0110
0101
0100
0011
0010
0001
0000
-0
-1
-2
-3
-4
-5
-6
-7
-8
1000
1001
1010
1011
1100
1101
1110
1111
-
1111
1110
1101
1100
1011
1010
1001
1000
-
1111
1110
1101
1100
1011
1010
1001
1000
CS2100
35
COMPLEMENT ON
FRACTIONS
We can extend the idea of complement on
fractions.
Examples:
CS2100
36
2s COMPLEMENT
ADDITION/SUBTRACTION (1/3)
Algorithm for addition, A + B:
1.
2.
3.
1.
2.
CS2100
Take 2s-complement of B.
Add the 2s-complement of B to A.
37
OVERFLOW
CS2100
38
2s COMPLEMENT
ADDITION/SUBTRACTION (2/3)
0011
+ 0100
------0111
-------
-2
+ -6
----8
----
1110
+ 1010
------11000
-------
+6
+ -3
---+3
----
0110
+ 1101
------10011
-------
+4
+ -7
----3
----
0100
+ 1001
------1101
-------
CS2100
39
2s COMPLEMENT
ADDITION/SUBTRACTION (3/3)
1101
+ 1010
------10111
-------
+5
+ +6
---+11
----
0101
+ 0110
------1011
-------
CS2100
40
1s COMPLEMENT
ADDITION/SUBTRACTION (1/2)
Algorithm for addition, A + B:
1.
2.
3.
1.
2.
CS2100
Take 1s-complement of B.
Add the 1s-complement of B to A.
41
1s COMPLEMENT
ADDITION/SUBTRACTION (2/2)
Any overflow?
CS2100
0011
+ 0100
------0111
------1101
+ 1010
-----10111
+
1
-----1000
+5
+ -5
----0
----
0101
+ 1010
------1111
-------
-3
+ -7
----10
----
1100
+ 1000
------10100
+
1
------0101
42
QUICK REVIEW
QUESTIONS (4)
DLD pages 42 - 43
Questions 2-13 to 2-18.
CS2100
43
EXCESS REPRESENTATION
(1/2)
Besides sign-and-magnitude and
CS2100
Excess-4
Representation
Value
000
-4
001
-3
010
-2
011
-1
100
101
110
111
44
EXCESS REPRESENTATION
(2/2)
Example: For 4-bit numbers, we may use excess-7 or
Value
Excess-8
Representation
0000
-8
1000
0001
0010
0011
0100
0101
0110
0111
CS2100
Value
1001
1010
1011
1100
1101
1110
1111
Number Systems and Codes
45
binary point
CS2100
46
integer part
CS2100
47
CS2100
48
mantissa
exponent
Trade-off:
CS2100
49
CS2100
11010
0011
50
CS2100
11000
1101
If exponent is in 1s complement.
11000
1110
If exponent is in 2s complement.
11000
0110
If exponent is in excess-8.
51
QUICK REVIEW
QUESTIONS (5)
DLD page 43
Questions 2-19 to 2-20.
CS2100
52
READING ASSIGNMENT
DLD page 31
CS2100
DLD pages 32 - 33
IEEE standard 754 floating point numbers:
http://steve.hollasch.net/cgindex/coding/ieeefloat.html
53
DECIMAL CODES
CS2100
54
CS2100
BCD
digit
0
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
55
CS2100
56
BCD
8421
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
Excess-3
84-2-1
2*421
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
0000
0111
0110
0101
0100
1011
1010
1001
1000
1111
0000
0001
0010
0011
0100
1011
1100
1101
1110
1111
Biquinary
5043210
0100001
0100010
0100100
0101000
0110000
1000001
1000010
1000100
1001000
1010000
CS2100
57
SELF-COMPLEMENTING
The codes representing the pair of complementary digits
CODES
0100
0101
0110
0111
1000
1001
1010
1011
1100
CS2100
58
CS2100
Binary
0000
0001
0010
0011
0100
0101
0110
0111
Gray Code
0000
0001
0011
0010
0110
0111
0101
0100
Decimal
8
9
10
11
12
13
14
15
Binary
1000
1001
1010
1011
1100
1101
1110
1111
Gray code
1100
1101
1111
1110
1010
1011
1001
1000
59
0
0
0
0
0
0
1
0
1
0
0
1
1
0
0
0
0
0
0
1
0
1
0
1
0
1
1
1
0
0
0
1
1
0
1
0
0
1
0
1
0
1
0
1
0
1
0
1
0
1
1
1
1
1
0
0
0
0
0
0
1
1
1
1
0
0
0
1
1
0
0
1
1
0
CS2100
60
0
0
0
11 01
000 0
000 00 1
0 1
0
mis-aligned
sensors
11
111
11
010 1
1
11 1
1
0
mis-aligned
sensors
0
11 00
110 1
000 1 0
11 0
1
0
00 01
11 1
0
000 11 11
1
0
0
11 01
111 0
111 0 1
00 1
0
0
00 00
001 0
111 00 0
0 0
1
1
1
010
1
11
01
010 0
1
11 1
1
0
1
11 11
00 0
0
000 00 11
0
0
0
1
sensors
61
READING ASSIGNMENT
CS2100
62
QUICK REVIEW
QUESTIONS (6)
DLD pages 43 - 44
Questions 2-22 to 2-25.
CS2100
63
ALPHANUMERIC CODES
(1/3)
Computers also handle textual data.
Examples
CS2100
64
ALPHANUMERIC CODES
(2/3)
ASCII
CS2100
American Standard
Code for Information
Interchange
7 bits, plus a parity bit
for error detection
Odd or even parity
Character
0
1
...
9
:
A
B
...
Z
[
\
ASCII Code
0110000
0110001
...
0111001
0111010
1000001
1000010
...
1011010
1011011
1011100
65
ALPHANUMERIC CODES
(3/3)
A: 1000001
ASCII table
LSBs
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
CS2100
000
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
O
SI
001
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
010
SP
!
#
$
%
&
(
)
*
+
,
.
/
MSBs
011 100
0
@
1
A
2
B
3
C
4
D
5
E
6
F
7
G
8
H
9
I
:
J
;
K
<
L
=
M
>
N
?
O
101
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
110
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
111
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
DEL
66
CS2100
Decimal
digit
Biquinary
5043210
0100001
0100010
0100100
0101000
0110000
1000001
1000010
1000100
1001000
1010000
67
Parity bit
CS2100
Character
0
1
...
9
:
A
B
...
Z
[
\
ASCII Code
0110000 1
0110001 0
...
0111001 1
0111010 1
1000001 1
1000010 1
...
1011010 1
1011011 0
1011100 1
68
Parity bits
0110 1
0001 0
1011 0
1111 1
1001 1
0101 0
Column-wise parity
Row-wise parity
CS2100
69
CS2100
70
CS2100
71
CS2100
72
CS2100
73
CS2100
74
Self-study
ERROR CORRECTION (5/7)
CS2100
75
Self-study
ERROR CORRECTION (6/7)
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
76
Self-study
ERROR CORRECTION (7/7)
Corrected data: 0 1 1 1 0 0 1 0 1 0 1 0
CS2100
77
END
CS2100
78