You are on page 1of 36

ESE218 Lecture 2

Binary numbers. Binary arithmetic. Negative


numbers.

Outline

Conversion from decimal to


binary
Integers
Fractions
Accuracy of conversion
Hexadecimal numbers
Binary arithmetic
Addition and subtraction,
Multiplication and division
Presentation of signed
integers
1s complements
2s complements
Summary

Binary number formats


Unsigned integers: 8 bits: 0255 16 bits: 065535
28-1
216-1
Signed integers: the SIGH BIT occupies one digit position
=> the magnitude range for signed numbers = of that for unsigned ones
Signed integers:
8 bits -128127 16 bits: +32767 -32767
Floating point*:
Single precision: 32 bits for sign, exponent and mantissa
S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
01
8 9
31
Sign - Exponent (8 bits) - Mantissa (23 bits)
Range: 2 -1282127 up to ~1038
Double precision: 64 bits for numbers up to ~10308
*In this course we will work with integer numbers only

POSITIONAL WEIGHTED number system:


consider (m+1)+n digits ama-n with a fixed binary point

The binary point separates the integer and fractional parts

.a a

a =

N= amam-1....a1ao
-1 -2 -n
Integer part: m+1 digits

Fractional part: n digits

Radix (base)=
2
m

1
0
N= am 2 + am-1 2m-1 + aExample:
2
+
a
2
1
0
-2
-n
1101.011
i -1 + a
+
a
2
2
+a
2
N= (ai-1 2 )
-2
-n
i=-nm

ai = 0 or 1

23 22

20

2-2 2-3

m=3

n=3

m, n positive integers
3

DIGIT WEIGHT = BASE

one of a kind is available

23+22+20
8

Assign:

1 - if the calibration weight is on the balance scale


0 - otherwise
4

Bit position reflects its weight


1

12

= 23 + 22 + 20= 1310

Weight increases from right to left

Digit=1 the weight is included into the sum

Digit=0 the weight is not included

1
5

Converting integer numbers


from decimal to binary
1. Take the decimal number and divide it by 2.
Write the remainder, it is the LSB of the binary form.
(LSB=0 if the decimal number was even and 1 otherwise).
2. Take the integer part of the result of division.
Repeat division by 2, record the remainder to the left of LSB.
Repeat the process continuously.

Division by 2 = scaling down the weight=


shifting binary point to the left

13 =

1101. =

8+4+1

6.5 = 110.1 =

4 + 2 + 0.5

3.25 = 11.01 =

2 + 1 + 0.25
7

INTEGER numbers converted to binary


by repeated division by 2

Conversion of integer number to binary form


Integer part: recording the remainders from right to left

Integer(13/2)=6, remainder 1 (least significant bit)


first
Integer(6/2)=3, remainder 0
Integer(3/2)=1, remainder 1
Integer(1/2)=0, remainder 1 (most significant bit)
1310 = 11012 = 1 23 + 1 22 + 0 21+ 1 20
MSB

LSB
8

one can skip the division

when you are ready to write the rest of a number


218/2= 109 remainder 0 = LSB
109/2= 54, remainder 1

Decimal
(base=10)
10
digits

9
8
7
6
5
4
3
2
1
0

Binary (base=2)
2
digits

27/2=13, remainder 1

1
0

13 = 1101

1 Bit = Binary digit


1 Byte = 8 bits

54/2= 27, remainder 0

No need to convert 13
since we remember the result

1 1 0 1 1 0 1 02 = 27 + 26 + 24 + 23 + 21
7

6 5

3 2

1 0

<-positions of the binary digits

21810 = 2 102 + 1 101 + 8 100


9

Fractional parts

Decimal numbers

10-1 10-2

.7

7 0.1 + 5 0.01

Binary numbers

2-1 2-2 2-3 2-4

.1 1

+
10

Fractional part
of a number converted to binary
by x2
1. Multiply the fractional part of a number by the base

record the integer part


2. Discard the integer part of the result.
Repeat continuously
11

Example: present 0.37510 in binary form

Fractional part: repeated multiplication by 2,

recording and discarding integer parts from left to right

0.37510 = 0.0112 = 0 2-1 + 1 2-2 + 1 2-3


2
0.7510

2
1.510
Discard that integer part
for the next iteration
0.5 x 2 =1.010

0.375 =

0.25 + 0.125

12

Conversion error
Rational numbers

for the fractional part

Pattern 1100 repeats infinitely


0.310=0.0100110011001100110011001100112
There will be an error in presentation with fixed number of bits

How many bits to keep for a given conversion error?


n number of digits
. a-1 a-2 a-n = a-1 2-1 + a-2 2-2 +a-n2-n

Error (E) = 2-n


Taking decimal logarithm

n > - (1/log 2) log (E) -3.3 log (E)


Representation of a decimal number with the accuracy of 110-3 = 0.1%
would requires -3.3x(-3) 10 binary digits.
Example:
A 24-bit DAC operates with a 1V reference voltage. The min voltage step
at the DAC output is 1/224 = 1/210 /210/24 1/103/103/16 V =1/16 V 60 nV
13

Decimal-to-Binary Conversion
To be performed separately for integer and fractional parts

Continuous
division by 2

Continuous
multiplication by 2

1. Starting from the LSB


assign
0 if the remainder = 0
1 if the remainder = 1
2. Discard the
remainder and divide
the result by 2

1. Starting from the


MSB assign
0 if the integer part =
0
1 if the integer part =
1
2. Discard the integer
14
part

Binary form is fine for computer


and too long for human
How to present long binary numbers
in a compact form?
and still to avoid time-consuming conversion to a different base

Use number systems with the base


equal to a power of 2:
Octal (base =8), Hexadecimal (base =16)
15

Integers with bases 10, 2 and 16


Decimal

Binary

Hexadecimal

10

11

100

101

110

111

1000

1001

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

Decimal

Binary

Hexadecimal

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

16

Conversions between binary


and hexadecimal numbers
21810 = 1 1 0 1 1 0 1 02 = 27 + 26+
76 43 1
= (23 + 22+

24

+ 23 +

20) 24 + 23 +

21
21

1 1 0 1 1 0 1 02 = (1101) 24 +(1010) 20
1 1 0 1 1 0 1 02 =

D 24 + A 20 = DA16
17

Hexadecimal numbers
with fractional parts

218 10 = 0000110110102 = 0DA16


0.375 10 = .011 = .01102 = 0.616
0.3 10 = .01001100112 =
= .0100110011002 =.4CC16
18

Binary arithmetic

11+10 = ?
There are 10 kinds of people:
1) Those who understand binary notation,
and
2) those who do not
19

Carries in addition
Borrows in subtraction

11
+ 10
101
x
Sum =0
Carry=1

x2

1
11
+
01
100

C1

Cin =

10
01
01
Difference =1,
Borrow =1

Sum =0, Cout=1

C1 = carry-out for the bit with index 0=


= carry-in for the bit with index 1

20

HALF-ADDER is a 2input circuit


generating SUM and CARRYOUT
Two Inputs

X
Y
COUT Sum

Two outputs

COUT

Sum

The first demonstration of use of relays


for implementation of arithmetic operations
11

George Stibitz
(1904-1995)
Logical 1

13

2
Vdc

5
1
X

9
6

High voltage (Vdc)


Low voltage (Ground)

Logical 0

5
1

SUM

4
8

0 1

16
1

High voltage applied to X and/or Y switches the relays

21

CARRYOUT

FULL ADDER is a 3-input circuit:


3rd input is reserved for CARRY-IN
Full addition for a binary digit (bit) with index i involves 3 operands (X,Y, Cin)
and produces 2 results: (Sum, Cout), C out for digit i is Cin for digit with index i+1
Carry-out

Carry-in

Xi

Yi

CIN
=

C C C
+ 3 2 1
X X X X
+ 3 2 1 0
Y3 Y2 Y1 Y0
C4 S3 S2 S1 S0
carry-out for digit 1=
= carry-in for digit 2

COUT=
Ci+1

Sum
Si

Ci

X
Y
BIN
BOUT Diff

Bin

BOUT

Diff

borrows

In hardware full subtraction is implemented by addition of a negative number: X+ (-Y),


the system converts + Y to Y and uses Adder, this simplifies hardware
22

MULTIPLICATION
by Addition and LEFT SHIFT

P4

X 2 X1
Y1

X0
Y0

X 2Y0 X 1Y0 X 0Y0

X 2Y1 X 1Y1 X 0Y1


P3

P2

P1

1 1 0
x
1 1
__________
1 1 1 0
Carry=
+ 1 1 0
____________
1 0 0 1 0

Left shift of number =


multiplication by 2 (binary point stays)
P0 Shift of all digits to the left by one position

increments the weight and it is equivalent


Length of the result:
to multiplication by the base or radix = 2
3-digit X + 2-gidit Y = 5-digit product

23

Division by subtraction and right shift

24

Division by subtraction and right shift


Ex: 14 4 = 3.5 in binary

1 1.1
quotient

1 0 02
divisor

1 1 1 0. 2
100
1 1 0.
- 100
-

1 0. 0
10 0
0

dividend

Right shift
=division by 2
Division by 2 is equivalent to right
shift of all digits by one position
25

Negative numbers in digital systems

26

Negative numbers use the 2s complement form:


1s complements = all bits inverted
2s complement = 1s complement +1
Consider 8-bit signed integers: sign occupies one bit position
=> 8-bit signed numbers range is of that of 8-bit unsigned numbers
All bits Inverted, incremented result = negative number in 2 s complement form
Positive number N

1s complement of N

2s complement of N = -N

01111111

+127 10000000

10000001

-127

01111110

+126 10000001

10000010

-126

+1

00000010

+2

111111101

11111110

-2

00000001

+1

111111110

11111111

-1

00000000

111111111

00000000

27

Addition of any number N to its 1s complement


results in the number with all bits equal 1,
if one considers 1s complement of number N for use
as N it would lead to minus zero =111..11
Ex: 3-bit number 111 could play the role of zero
Number N

1s complement of N

N + 1s c. of N
=minus zero

0= 0 0 0

111

111

+1= 0 0 1

110

111

+2= 0 1 0

101

111

+3= 0 1 1

100

111

011
010
001
000

Presence of two zeros


111
in the 1s complement system
110
makes it inconvenient
-1
101
for presentation
-2
1
0
0
of negative numbers
-3

+2

+3

+1

-0
101
100

28

2s complements

obtained by incrementing 1s complements


Number N

1s complement of N

2s c. of N =
1s c. of N +1

0= 0 0 0

111

000

+1= 0 0 1

110

111 =-1

+2= 0 1 0

101

+3= 0 1 1

100

+1

110 =-2

011

101 =-3

010
001

100 =-4
000
The 2s complement system of
111
presentation of negative
110
-2
numbers has a single zero
101
-3
100
-4

+2

+3

+1

-1
101
100

29

signed magnitude form would be


too complicated to handle
With the signed magnitude notation binary equivalents of decimal numbers
would appear as shown in the table
To perform addition of numbers with sign we need
Positive N

Negative N

+0

000

-0

100

+1

001

-1

101

+2

010

-2

110

+3

011

-3

111

to analyze signs of X and Y


1) Both positive or both negative =>
Results has the same sign, Magnitude
= X+Y
2) Different signs => Compare magnitudes
if X > Y then Sign=X, Magn = X - Y
if Y > X then Sign=Y, Magn = Y - X

Drawbacks of the signed magnitude notation:


1. Complicated algorithms
2. Different algorithms for addition
and subtraction
3. Two zeros: 0 and - 0!

In digital systems the SIGNED MAGNITUDE FORM IS NOT USED


30

Advantage of 2s complement systems: no need to implement subtractor in hardware

Subtraction of two numbers M N


is implemented by addition M +(-N)
(M, N are signed integers)

In a binary system with 3 bits the decimal range is from - 4 to +3

Consider M-N =2-3=2+(8-3)=2+5=7


111 (or 7) represents -1

start

end

011 = +3
010 = +2
001 = +1
000 = 0
111 = -1
110 = -2
101 = -3
100 = -4

+1step

-3

in 3-digit presentation
8=23 = 10002 = 0

addition of +5

<=>

011
010
001
000
111
110
101
100

8 - 3 = +5

A 3-bit binary system: only 8 steps Sign bit

subtraction of 3 =
= 3 steps down from +2

+4steps
In 3-bit system 8=0, a number does not change if one adds 23 to31it

In a 3-bit system the number range is -4 +3


8= 0, 7= -1, 6= -2, 5= -3, 4= -4
3-bit binary system: 23 = 0 subtraction
a2a1a0 + 23 = a2a1a0
- N = 2s c. of N = 2n N

4-th bit
discarded

addition
0

-1

000
111

001 = 1
+1
111 = 7
-1
Positive N
Negative N
1000 = 0
0
0

000

-1=7

111

+1

001

-2=6

110

+2

010

-3=5

101

+3

011

-4=4

100

-2

-3

+1

001

010

110
101

+2

011
+3

100
-4
32

In digital systems the negative number is obtained from the


positive number by inversion of all bits and incrementing result

2s c. of N = 2n N
+

001
110
111

original number
1s c. of it (by digit inversion)

1s c. of N = 2n 1 N

all 1s -1

2s c. = 1s c. + 1
Positive N

Negative N

000

-0

111

001

-1

110

010

-2

101

011

-3

100

To get 2s c. we just need to


add 1 to 1s complement
obtained by inversion
of all N digits

33

2s complements obtained fast!


1. Keep all rightmost zeros and the first 1 from the right
2. Complement the remaining part of a number

Examples:
A = 0011100
-A = 1100100
B = 0011110
-B = 1100010

C = 000001
- C = 111111
D = 100000
-D = 100000

34

Interpretation of the magnitude


For positive numbers the magnitude is determined directly by addition of weights :
+13 = 00001101
(The magnitude range for signed 8-bit numbers is -128 +127)
12+4+1 =13
For negative numbers the magnitude is presented in the 2s complement form,
one has to be obtain the 2s complement of the negative number then add the weights
-13 = 11110011 2s complementing

+13 = 00001101
12+4+1 =13

Example: subtraction by addition of the 2s complement (assume 8-bit form)


9 -12 = +9 (+12) = 9 + (-12)
+12 = 00001100
-12 = 11110100

Negative
result

00001001
+1 1 1 1 0 1 0 0
--------------------- Magnitude by 2s c.-ting
1 1 1 1 1 1 0 1 =>2s c. => 00000011 = 310
35

Summary
1. Digital systems operate with BINARY numbers and codes while designers work with their compact
representation, HEX numbers
2. Perform decimal-to-binary conversion separately for integer and fractional parts
- divide-by-2 the integer part
- multiply-by-2 the fractional part, the max error is 2 -n
3. Results of addition and subtraction differ in Carry and Borrow bits
4. Multiplication and division can be performed by successive addition and subtraction, respectively
(and shift)
5. 2s complement representation eliminates the need in separate hardware for subtraction, i.e.
subtraction is performed by addition of a negative number presented in the 2s complement form
6. 1s complement is obtained by inversion of all bits
7. 2s complement is obtained from 1s complement by addition of 1

36

You might also like