0% found this document useful (0 votes)
76 views

Binary Representation

1) Binary representation uses only the digits 0 and 1, with the most significant bit representing the sign (0 for positive, 1 for negative). Numbers are stored in two's complement format. 2) Conversions between numeral systems involve grouping bits into nibbles for hexadecimal, or repeatedly taking remainders when dividing by the new base for other bases. 3) Negative numbers are represented by inverting all bits and adding 1 to the magnitude in two's complement notation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Binary Representation

1) Binary representation uses only the digits 0 and 1, with the most significant bit representing the sign (0 for positive, 1 for negative). Numbers are stored in two's complement format. 2) Conversions between numeral systems involve grouping bits into nibbles for hexadecimal, or repeatedly taking remainders when dividing by the new base for other bases. 3) Negative numbers are represented by inverting all bits and adding 1 to the magnitude in two's complement notation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Digital

Di it l IIntegrated
t t d Ci
Circuits
it &
Microcontrollers

Chapter 2. Binary representation

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

Conversion between numeration systems

 Conversion from binary into hexadecimal


 Group into 4 bits (nibbles)
 Each nibble corresponds to a hexadecimal symbol:

1101101,10011012

0110 1101,1001 10102

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.

i  d k b k  dk -11 bk -1  ...  d 3b3  d2 b 2  d1b1  d0 b0

Conversion between numeration systems


 C
Conversion
i from
f decimal
d i l into
i t base
b b
 For the fractional part:
f  d 1 b1  d 2 b 2  d 3 b 3  ...  d  k b  k  ...
 Multiply f with b
b  f  d 1  d 2 b 1  d 3 b 2  ...  d  k b  k 1  ...
 Keep the integer part from the right part, d–1, which is
subtracted from the left part.
 Continue by multiplying the remaining fractionary part to b until
reach 0.

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

Conversion between numeration systems


 Th fractional
The f i l part:
0.65  2 = 1.3 1 MSB
03
0.3  2 = 0.6
06 0
0.6  2 = 1.2 1
02
0.2  2 = 0.4
04 0
0.6510=0.10(1001)2
0.4  2 = 0.8 0
0.8  2 = 1.6 1
0.6  2 = 1.2 1
0.2  2 = 0.4 0 ...
 Results that number 0.65 cannot be exactly represented on a
finite number of bits.
bits

8
Negative numbers representation

 MSB – sign bit.


0 for positive numbers (+);
 1 for negative numbers (–).

 The rest of N–1 bits are for value representation.

2N–2 ... 20
s m
1 bit  N–1 bits 

Negative numbers representation

 The representation: sign bit, magnitude


 EXAMPLE:
9 = 0 01001
–9
9 = 1 01001
 The range of representation:
 2NN–11 positive values between 0 and 2NN–11–1.
1
 2N–1 negative values between –(2N–1–1) and 0.

2N–2 ... 20
s m
1 bit  N–1 bits 

10
Negative numbers representation

 Two’s complement representation


 The negative numbers representation is obtained by
addition of 2N.
 EXAMPLE:
EXAMPLE
 For N=6 bits (2N = 64).
13 = 0011012
–13 corresponds to 64 + (–13) = 51 = 1100112

11

Two’s complement representation

 For obtaining negative numbers:


 Each bit is complemented;
 Add 1.
 EXAMPLE:
 for 13 = 0011012
complement each bit: 1100102
add 1: 1100112 = –13
 for –13 = 1100112
complement
l t each
h bit
bit: 0011002
add 1: 0011012 = +13

12
Two’s complement representation

 The range of two’s complement representation


 2N–1 positive values between 0 and 2N–1–1.
 2N–1 negative values between –2N–1 and -1.

 The result of adding a number with its two’s


complement is 0:

13+ 0011012
–13 1100112
=0 10000002

13

Integer numbers representation

 Binary representation is considered right


aligned (decimal point is right to LSB).
 MSB represents the sign bit.
–2
2N–1 2N–2 ... 20
s i .
 N biţi 

 Integer two’s complement range:

2 N 1 ,..., 1,0,...,2 N 1  1

14
Integer numbers representation

 EXAMPLE: (for N=4 bits)


Decimal Binary Decimal Binary
0 0000 -8 1000
1 0001 -7
7 1001
2 0010 -6 1010
3 0011 -5
5 1011
4 0100 -4 1100
5 0101 -3
3 1101
6 0110 -2 1110
7 0111 -1 1111

15

Integer numbers representation

 Sign integers addition


1111 011 11
+3 0011 –5 1011 –3 1101
–2 1110 +3 0011 –4 1100
1 0001 –2 1110 –7 1001
 An overflow occurs if the result is outside of the N bits
representation range:
+3 0011 –3
3 1101
+6 0110 –6 1010
9 1001=–7 –9 0111 =7

16
Integer numbers representation

 Sign bit extension


 Needed when increasing the number of bits for the
integer part.
 Sign
Si bit iis copied
i d tto th
the lleft
ft ttoward
d MSB
MSB.

N=4 biţi N’=8 biţi


+3 0011 +3 0000 0011
–3 1101 –3 1111 1101

17

Integer numbers representation

 Multiplying by a power of 2
 Multiplying by 2k is equivalent with shifting left k
bits and filling with 0 toward LSB.

N=8 biţi N=8 biţi


3 0000 0011 –3 1111 1101
3.22 0000 1100 –3.22 1111 0100

18
Integer numbers representation

 Dividing by a power of 2
 Dividing by 2k is equivalent with shifting right k bits
and sign bit extension.

N=8 biţi N=8 biţi


24 0001 1000 –24
24 1110 1000
24/23 0000 0011 –24/23 1111 1101

19

Integer numbers representation

 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

 Multiplication is equivalent with consecutive shift


and
d add
dd operations.
ti
 For example 5 can be expressed:
5  20  2 2
 Multiplication can be computed:
6  5  6   20  2 2   6  1  6  2 2

61 0110
622 0110 +
0011110

21

Integer numbers representation

 Integers division
 Successive subtractions of the divisor from the
dividend.

15| 3 1111 11
5 –11 101
0011
–11
00

22
Fractional numbers representation

 A fractional part f is any number who’s modulus


satisfies
ti fi ththe iinequality:
lit
0.0  | f |  1.0
 Left aligned: binary point is at the right of MSB
–20 2–1 2–2 ... 2–B
s. f
1bit  B=N–1 bits 

 Fixed point fractional representation range:


1, ..., 2 B ,0, 2 B...,1  2 B
23

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

Fractional numbers representation

 Quick conversion of fractional numbers into


binary
f represented on N=B+1 bits is an integer multiple of
2–BB 0 –2
–1 –22 2 ...
–B 2
s. f

 Let i the corresponding integer multiple:


i  f  2B
 Equivalent with a left shifting of f with B bits.

26
Fractional numbers representation

–20 2–1 ... 2–B


s . f

–2B 2B–1 ... 20


s i .
 N bits 

 Converting f from binary to decimal


 consider the binary representation for the corresponding
signed integer i and then, divide by 2B

27

Fractional numbers representation

 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

 For: 1.110 1100


two’s
two s complement: –0001
0001 0100 = –20
2010
divide by 27=128: –20/128 = –0.15625

28
Fractional numbers representation
–20 2–1 ... 2–B
s . f

–2B 2B–1 ... 20


s i .
 N biţi 

 Converting f from decimal to binary


 multiply f by 2B (shift left B bits)
 Represent the integer part i as a signed integer on N bits.

29

Fractional numbers representation

 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

 For 0.65 (N=8 bits, B=7)


multiply by 27: 0.65.128= 83.210
t k the
take th integer
i t part:
t 8310 = 010100112

 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

Floating point representation


 The following are equivalent
representations of 1,234

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

IEEE 754 Standard

 Single precision: 32 bits, consisting of...


 Signbit (1 bit)
 Exponent (8 bits)
 Mantissa (23 bits)

 Double precision: 64 bits, consisting of…


 Signbit (1 bit)
 Exponent
p ((11 bits))
 Mantissa (52 bits)

34
Single Precision Format

32 bits

Mantissa (23 biţi)

Exponent (8 biţi)

Sign of mantissa (1 bit)

35

Normalization

 The mantissa is normalized


 Has an implied decimal place on left
 Has an implied “1” on left of the decimal place

 E.g.,
 Mantissa 10100000000000000000000
 Represents… 1.1012 = 1.62510

36
Excess Notation

 To include +ve and –ve exponents, “excess”


notation
t ti isi used
d
 Singleprecision: excess 127
 Double precision: excess 1023

 The value of the exponent stored is larger than


the actual exponent
 E.g.,
g , excess 127,,
 Exponent  10000111
 Represents… 135 – 127 = 8
37

Example
 Si l precision
Single i i
0 10000010 11000000000000000000000

1 112
1.11

130 – 127 = 3

0 = positive mantissa

+1.112 x 23 = 1110.02 = 14.010

38
Hexadecimal

 It is convenient and common to represent the


original
i i l flfloating
ti point
i t number
b iin h
hexadecimal
d i l
 The preceding example…

0 10000010 11000000000000000000000

4 1 6 0 0 0 0 0

39

Converting from Floating Point

 What decimal value is represented by the


following
o o g332-bit
b t floating
oat g po
pointt number?
u be

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

Converting from Floating Point

 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

Converting from Floating Point

 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

 Express 36.562510 as a 32-bit floating point


number (in hexadecimal)

45

Converting to Floating Point

 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

Converting to Floating Point

 Step 3
 Determine S, E, and M

+1.0010010012 x 25
S M
n E = n + 127
= 5 + 127
= 132
= 100001002

S = 0 (because the value is positive)

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

Converting to Floating Point

 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

You might also like