Binary Representation
Binary Representation
Di it l IIntegrated
t t d Ci
Circuits
it &
Microcontrollers
Numeration Systems
Decimal
the set of symbols is:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
The integer number 493 in base 10:
49310 = 4•102 + 9•101 + 3•100 =
= 400 + 90 + 3
The rational number 35,54 in base 10:
35.6410 = 3•101 + 5•100 + 6•10-1 + 4•10-2 =
= 30 + 5 + 0 0.6
6 + 0
0.04
04
2
Numeration Systems
Binary
the set of symbols is:
{0, 1};
110012 = 1 1•2
24 + 1
1•2
23 + 0
0•2
22 + 0
0•2
21 + 1
1•2
20 =
= 16 + 8 + 1 = 25
110.012 = 1•22 + 1•21 + 0•20 + 0•2-1 + 1•2-2=
= 4 + 2 + 0.25 = 6.25
1101101,10011012
6 D , 9 A16
4
Conversion between numeration systems
Conversion from decimal into base b
For the integer part we can write:
i d b d
k k -1 b ... d3 b d2 b d1 b d0
d0 is the remainder of division of i to b
The quotient (another integer) is divided to b
Repeat until reach 0.
The remainder obtained after each division is the symbol dk of
representing into the base b.
b b f d 1 =d 2 d 3 b 1 ... d k b k 2 ...
6
Conversion between numeration systems
EXAMPLE
EXAMPLE:
Represent the number 23.65 into base 2:
Th integer
The i t part:
t
23 : 2 = 11 1 LSB
11 : 2 = 5 1
5:2= 2 1
2:2= 1 0
1:2= 0 1 MSB
2310 = 101112
8
Negative numbers representation
2N–2 ... 20
s m
1 bit N–1 bits
2N–2 ... 20
s m
1 bit N–1 bits
10
Negative numbers representation
11
12
Two’s complement representation
13+ 0011012
–13 1100112
=0 10000002
13
2 N 1 ,..., 1,0,...,2 N 1 1
14
Integer numbers representation
15
16
Integer numbers representation
17
Multiplying by a power of 2
Multiplying by 2k is equivalent with shifting left k
bits and filling with 0 toward LSB.
18
Integer numbers representation
Dividing by a power of 2
Dividing by 2k is equivalent with shifting right k bits
and sign bit extension.
19
Integers multiplication
unsigned integers
The result in double
6 0110
precision representation 5 0101
Binary multiplication with 0110
0 and 1 0000
0110
__ 0000 +
30 0011110
20
Integer numbers representation
61 0110
622 0110 +
0011110
21
Integers division
Successive subtractions of the divisor from the
dividend.
15| 3 1111 11
5 –11 101
0011
–11
00
22
Fractional numbers representation
EXAMPLE:
for 4 bits
N=4
N
Decimal Binary Decimal Binary
0 0000 -1 1000
000
0.125 0001 -0.875 1001
0.250 0010 -0.750 1010
0.375 0011 -0.625 1011
0.500 0100 -0.500 1100
0.625 0101 -0.375 1101
0.750 0110 -0.250 1110
0.875 0111 -0.125 1111
24
Fractional numbers representation
Qm.n format
n bits for fractional part;
p
(optional) specify the number of bits m for the integer
part, excluding the sign bit (MSB);
The complete binary representation has 1+m+n bits.
EXAMPLE (for N=16 bits):
Q15 means 15 bits for the fractional part (16 bits with the sign
bit)
Q1.14
Q1 14 has 1 bit for the integer part
part, 14 bits for the fractional part
and the sign bit.
25
26
Fractional numbers representation
27
EXAMPLE:
Binary to decimal (N=8 bits, B=7),
For: 0.010 0110
d i l iinteger:
decimal t 0010 0110 = 3810
divide by 27=128: 38/128 = 0.296875
28
Fractional numbers representation
–20 2–1 ... 2–B
s . f
29
EXAMPLE:
Decimal to binaryy (N=8
( biţi,
ţ B=7))
For: 0.875
multiply by 27: 0.875.128=11210
represented binary: 11210 = 011100002
For: –0.625
multiply by 27: –0.625.128= –8010
represented in 8010 = 010100002
two’s complement: –8010 = 101100002
30
Fractional numbers representation
In
I the
th last
l t example
l the
th quantization
ti ti error (by(b
truncation) appears since the fractional 0.65 cannot
be exactly represented on 8 bits
bits.
The result 0.10100112 is equal to 0.6484375.
The q
quantization error is: 0.65
- 0.6484375
= 0.0015625
31
123,400.0 x 10-2
12 340 0
12,340.0 x 10-11
The representations differ
1,234.0 x 100 in that the decimal place –
123
123.4 x 101 th “point”
the “ i t” – “floats”
“fl t ” to
t the
th
left or right (with the
12.34 x 102
appropriate
pp p adjustment
j in
1.234 x 103 the exponent).
0.1234 x 104
32
Parts of a Floating Point Number
Exponent
-0.9876
0 9876 x 10-33
Sign of
Sign of Location of exponent
mantissa decimal point Mantissa
Base
33
34
Single Precision Format
32 bits
Exponent (8 biţi)
35
Normalization
E.g.,
Mantissa 10100000000000000000000
Represents… 1.1012 = 1.62510
36
Excess Notation
Example
Si l precision
Single i i
0 10000010 11000000000000000000000
1 112
1.11
130 – 127 = 3
0 = positive mantissa
38
Hexadecimal
0 10000010 11000000000000000000000
4 1 6 0 0 0 0 0
39
C17B000016
40
Converting from Floating Point
Step 1
Express in binary and find S, E, and M
C17B000016 =
1 10000010 111101100000000000000002
S E M
1 = negative
0 = positive
41
Step 2
Find“real” exponent, n
n = E – 127
= 100000102 – 127
= 130 – 127
=3
42
Converting from Floating Point
Step 3
PutS, M, and n together to form binary result
(Don’t forget the implied “1.” on the left of the
mantissa.)
-1.11110112 x 2n =
-1.11110112 x 23 =
-1111.10112
43
Step 4
Express result in decimal
-1111.10112
-15
15 2-1 = 0.5
2-3 = 0.125
2-44 = 0.0625
0.6875
Answer: -15.6875
15.6875
44
Converting to Floating Point
45
Step 1
Express original value in binary
36.562510 =
100100.10012
46
Converting to Floating Point
Step 2
Normalize
100100 10012 =
100100.1001
1.0010010012 x 25
47
Step 3
Determine S, E, and M
+1.0010010012 x 25
S M
n E = n + 127
= 5 + 127
= 132
= 100001002
48
Converting to Floating Point
Step 4
Put S, E, and M together to form 32-bit
32 bit binary result
0 10000100 001001001000000000000002
S E M
49
Step 5
Express in hexadecimal
0 10000100 001001001000000000000002 =
0100 0010 0001 0010 0100 0000 0000 00002 =
4 2 1 2 4 0 0 016
Answer: 4212400016
50