Cs2100 2 Number Systems and Codes
Cs2100 2 Number Systems and 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
2011 Sem 1
Number Systems
Examples
Represent yes or no: use 0 and 1
Represent the 4 seasons: 0, 1, 2 and 3
2011 Sem 1
Number Systems
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
2011 Sem 1
Number Systems
2011 Sem 1
Number Systems
log 2 M
5
In general,
2011 Sem 1
an an1 L
a1a0 .b1b2 L
k 0
k 1
r ak r k bk r k
2011 Sem 1
Number Systems
2011 Sem 1
Number Systems
A bit of history
the Hindu-Arab Numeral System
From Wikipedia:
The symbols for 1 to 9 in the Hindu-Arabic numeral system evolved from the Brahmi
numerals. Buddhist inscriptions from around 300 BC use the symbols which became 1, 4
and 6. One century later, their use of the symbols which became 2, 7 and 9 was
recorded.
The first universally accepted inscription containing the use of the 0 glyph is first recorded
in the 9th century, in an inscription at Gwalior dated to 870. Indian documents on copper
plates, with the same symbol for zero in them, dated back as far as the 6th century AD,
abound.
The numeral system came to be known to both the Persian mathematician Al-Khwarizmi,
whose book On the Calculation with Hindu Numerals written about 825, and the Arab
mathematician Al-Kindi, who wrote four volumes, On the Use of the Indian Numerals
about 830, are principally responsible for the diffusion of the Indian system of numeration
in the Middle East and the West. In the 10th century, Middle-Eastern mathematicians
extended the decimal numeral system to include fractions, as recorded in a treatise by
Syrian mathematician Abu'l-Hasan al-Uqlidisi in 95253.
2011 Sem 1
Number Systems
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.
Base/radix R:
Weights in powers of R
2011 Sem 1
Number Systems
10
Number Systems
11
Easy!
1101.1012 =
572.68
2A.816
341.245 =
2011 Sem 1
Number Systems
12
DLD page 37
Questions 2-1 to 2-4.
2011 Sem 1
Number Systems
13
Method 1
Sum-of-Weights Method
Method 2
Repeated Division-by-2 Method (for whole numbers)
Repeated Multiplication-by-2 Method (for fractions)
2011 Sem 1
Number Systems
14
SUM-OF-WEIGHTS METHOD
2011 Sem 1
Number Systems
15
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
2011 Sem 1
Number Systems
16
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.
Carry
(0.3125)10 = (.0101)2
MSB
0.31252=0.625 0
1
0.6252=1.25
0
0.252=0.50
1
LSB
0.52=1.00
2011 Sem 1
Number Systems
17
Decimal to base-R
Whole numbers: repeated division-by-R
Fractions: repeated multiplication-by-R
2011 Sem 1
Number Systems
18
page 37
Questions 2-5 to 2-8.
2011 Sem 1
Number Systems
19
Base-R
Decimal
Base-2
Base-3
Base-4
.
Base-R
2011 Sem 1
Number Systems
20
2011 Sem 1
Number Systems
21
page 37
Questions 2-9 to 2-10.
2011 Sem 1
Number Systems
22
READING ASSIGNMENT
Binary
arithmetic operations
2011 Sem 1
Number Systems
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
2011 Sem 1
Number Systems
24
DEFINITIONS
Given a n-bit binary string: an 1an 2 L a1a0
Sign-and-magnitude interpretation:
n2
ai 2i
i 0
1s complement interpretation:
an1
n 1
n2
1 an 1 ai 2i
i 0
2s complement interpretation:
2
2011 Sem 1
n 1
n2
an 1 ai 2i
i 0
Number Systems
25
SIGN-AND-MAGNITUDE (1/3)
magnitude
sign
Number Systems
26
SIGN-AND-MAGNITUDE (2/3)
Largest value:
01111111 = +12710
Zeros:
Question:
00000000 = +010
10000000 = -010
2011 Sem 1
Number Systems
27
SIGN-AND-MAGNITUDE (3/3)
To negate a number, just invert the sign bit.
Examples:
2011 Sem 1
Number Systems
28
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
Number Systems
29
1s COMPLEMENT (2/3)
Essential technique to negate a value: invert all the bits.
Largest value:
01111111 = +12710
Smallest value:
Zeros:
2011 Sem 1
10000000 = -12710
00000000 = +010
11111111 = -010
Number Systems
30
1s COMPLEMENT (3/3)
2011 Sem 1
Number Systems
31
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 1s-complement is:
-000011002
= 28 12 (calculation in decimal)
= 244
= 111101002s
Number Systems
32
2s COMPLEMENT (2/3)
Essential technique to negate a value: invert all the
bits, then add 1.
Largest value:
01111111 = +12710
Zero:
2011 Sem 1
00000000 = +010
Number Systems
33
2s COMPLEMENT (3/3)
2011 Sem 1
Number Systems
34
COMPARISONS
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
2011 Sem 1
Number Systems
35
COMPLEMENT ON FRACTIONS
We can extend the idea of complement on fractions.
Examples:
2011 Sem 1
Number Systems
36
2s COMPLEMENT
ADDITION/SUBTRACTION (1/3)
2011 Sem 1
Number Systems
37
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
-------
2011 Sem 1
Number Systems
38
2s COMPLEMENT
ADDITION/SUBTRACTION (3/3)
1101
+ 1010
------10111
-------
+5
+ +6
---+11
----
0101
+ 0110
------1011
-------
2011 Sem 1
Number Systems
39
1s COMPLEMENT
ADDITION/SUBTRACTION (1/2)
2011 Sem 1
Number Systems
40
1s COMPLEMENT
ADDITION/SUBTRACTION (2/2)
2011 Sem 1
0011
+ 0100
------0111
------1101
+ 1010
-----10111
+
1
-----1000
Number Systems
+5
+ -5
----0
----3
+ -7
----10
----
0101
+ 1010
------1111
------1100
+ 1000
------10100
+
1
------0101
41
OVERFLOW
2011 Sem 1
Number Systems
42
Why it works?
Comes
The
2011 Sem 1
Number Systems
43
page 37
Questions 2-13 to 2-18.
2011 Sem 1
Number Systems
44
2011 Sem 1
Number Systems
Excess-4
Representation
Value
000
-4
001
-3
010
-2
011
-1
100
101
110
111
45
2011 Sem 1
Value
Excess-8
Representation
0000
1000
0001
1001
0010
1010
0011
1011
0100
1100
0101
1101
0110
1110
0111
1111
Number Systems
Value
46
binary point
2011 Sem 1
Number Systems
47
integer part
fraction part
assumed binary point
Number Systems
48
2011 Sem 1
Number Systems
49
sign
mantissa
exponent
Trade-off:
More bits in mantissa better precision
More bits in exponent larger range of values
2011 Sem 1
Number Systems
50
2011 Sem 1
11010
0011
Number Systems
51
2011 Sem 1
11000
1101
If exponent is in 1s complement.
11000
1110
If exponent is in 2s complement.
11000
0110
If exponent is in excess-8.
Number Systems
52
page 38
Questions 2-19 to 2-20.
2011 Sem 1
Number Systems
53
READING ASSIGNMENT
2011 Sem 1
Number Systems
54
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.
2011 Sem 1
Number Systems
55
Decimal
digit
BCD
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
2011 Sem 1
Number Systems
56
2011 Sem 1
Number Systems
57
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
2011 Sem 1
Number Systems
58
SELF-COMPLEMENTING CODES
The codes representing the pair of complementary digits are
also complementary to each other.
0: 0011
Example: Excess-3 code
1:
2:
3:
4:
5:
6:
7:
8:
9:
0100
0101
0110
0111
1000
1001
1010
1011
1100
2011 Sem 1
Number Systems
59
2011 Sem 1
Number Systems
60
0000
0001
0011
0001
0010
0000
0010
0110
0111
0011
0101
0001
0100
0000
1100
0100
0101
1101
0111
1111
0110
1110
0010
1010
0011
1011
0001
1001
0000
1000
2011 Sem 1
Number Systems
61
0
0
0
1
1
0
0
0
mis-aligned
sensors
mis-aligned
sensors
1
010 11
111
1 1
111 1 1
1
0
0
11 00
110 1
0
00 1 0
11 0
1
1
11 11
00 0
0
1
000 00 1
0
0
0
1 01
000 10
000 00 1
0 1
0
0
0 01
11 01
0
1
000 11 1
1
0
sensors
0
00 00
001 0
111 00 0
0
0
1
1
1
0
11 01
111 0
111 0 1
00 1
0
0
010 10
010
1 1
111 1 1
1
0
Number Systems
READING ASSIGNMENT
2011 Sem 1
Number Systems
63
page 38
Questions 2-21 to 2-24.
2011 Sem 1
Number Systems
64
alphabets:
A Z, a z
digits:
0 9
special symbols: $, ., @, *, etc.
non-printable: NULL, BELL, CR, etc.
Examples
ASCII (8 bits), Unicode
2011 Sem 1
Number Systems
65
ASCII
American Standard Code
for Information
Interchange
7 bits, plus a parity bit for
error detection
Odd or even parity
2011 Sem 1
Number Systems
Character
0
1
...
9
:
A
B
...
Z
[
\
ASCII Code
0110000
0110001
...
0111001
0111010
1000001
1000010
...
1011010
1011011
1011100
66
LSBs
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
2011 Sem 1
A: 1000001
table
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
Number Systems
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
67
Unicode
International
Number Systems
68
2011 Sem 1
Number Systems
69
Parity bit
Even parity: additional bit
added to make total
number of 1s even.
Odd parity: additional bit
added to make total
number of 1s odd.
2011 Sem 1
Character
0
1
...
9
:
A
B
...
Z
[
\
Number Systems
ASCII Code
0110000 1
0110001 0
...
0111001 1
0111010 1
1000001 1
1000010 1
...
1011010 1
1011011 0
1011100 1
Parity bits
70
0110 1
0001 0
1011 0
1111 1
1001 1
0101 0
Column-wise parity
Row-wise parity
2011 Sem 1
Number Systems
71
2011 Sem 1
Number Systems
72
END
2011 Sem 1
Number Systems
73