Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as...

49
Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal

Transcript of Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as...

Page 1: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Arithmetic operations in binary

Addition / subtraction

01011

+ 111

?

“Method” exatly the same as decimal

Page 2: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Arithmetic operations in binary

Addition

X = xn … xi … x0

+ Y = yn … yi … y0

__________________

di

di = (xi + yi ) mod r + carry-in

Page 3: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Addition

For ( i = 0…n ) do

di = (xi + yi + carry-in) mod r

carry-out = (xi + yi + carry-in) div r

End for

Page 4: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Arithmetic operations in binary

Subtraction Can you write an equivalent method

(algorithm) for subtraction?

Algorithm: a systematic sequence of steps/operations that describes how to solve a problem

Page 5: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Arithmetic operations in binary

SubtractionX = xn … xi … x0

Y = yn … yi … y0___________________________________________________

di

For ( i = 0…n ) do

di = ( xi yi bi-1 ) mod r

borrow-out = ( xi yi bi-1 ) div rEnd for

Page 6: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Subtraction

Page 7: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Arithmetic operations in binary

Multiplication

For ( i = 0…n ) do

di = ( xi yi + ci-1 ) mod r

ci = ( xi yi + ci-1 ) div rEnd for

Is this correct?

Page 8: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Negative numbers (4 traditions):

n = number of symbols (digits, bits, etc.)r = radixradix complement of x is

1)1( xrxr nn

e.g. n = 4, r = 107216 --> 9999 - 7216 + 1 = 2784 (10s complement)

n = 4, r = 20101 --> 1111 - 0101 + 1 = 1011 (2s complement)

Signed magnitudeRadix complementDiminished radix complementExcess-b (biased)

Page 9: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

xrn )1(

e.g. n = 4, r = 107216 --> 9999 - 7216 = 2783 (9s complement)

n = 4, r = 20101 --> 1111 - 0101 = 1010 (1s complement)

diminished radix complement is

Note: operation is reversible (ignoring overflow)i.e. complement of complement returns original pattern

(for both radix and diminished radix forms)

for zero and its complement:

10s complement: 0000 --> 9999 + 1 = 10000 --> 0000

9s complement: 0000 --> 9999 --> 0000

Page 10: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Arithmetic with complements n = 4r = 2

Excess-8

Page 11: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Signed number representations[[N]] = rn – ( rn – N ) = N

( N + [N] ) mod rn = 0 Example: (n = 4)+5 0101-5 1011 10000 24

N + [N] mod 24 = 0

N = 5 r = 10 N = 32546 [N] = 105 – 32546 = (67454)10

Page 12: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Two’s complement arithmetic

( an-1, an-2, … a0 ) in 2-s complement is

– Example: (n = 4)0101 023 + 122 + 02 + 11 = 4 + 1 = 5

1011 123 + 022 + 12 + 11 = 8 + 2 + 1 = 5

– Addition/subtraction (n = 5)

+10 01010 +3 00011 01101 13

2

0

11 22

n

i

ii

nn aa

Page 13: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Two’s complement arithmetic

– Addition/subtraction (n = 5)

+10 01010

+7 00111

Overflow 10001 15

+15 01010

13 10011

Discard 100010 2

When can overflow happen? Only when both operands have the same sign and the sign bit of the result is different.

Page 14: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Cyclic representation (n = 4, r = 2)avoid discontinuity between0111 and 1000

Add x:move x positions clockwise

Subtract x:move x positions counterclockwisemove (16 - x) positions clockwise(i.e. add radix complement)

Page 15: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

How to detect discontinuity?

5+ 6 11

0101+ 0110 1011

no overflow (in 4-bit register)but, carry into most significant bit is 1 while carry out is 0

-5+ -6 -11

1011+ 1010 10101

overflow but, carry into most significant bit is 0 while carry out is 1

Page 16: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Same circuitrysigned numbers

addsubtract (use 2s complement of subtrahend)Intel architecture OF (overflow flag) detects out-of-range result

unsigned numberssame protocolbut discontinuity is now between 0000 and 1111

detect with overflow for additionlack of overflow for subtractionIntel uses CF (carry flag) to detect out-of-range result

Page 17: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Codes

4-bit codes for digits 0 through 9 (BCD : 8421 weighted)2421 and Excess-3 are self-complementing

(complement bits to get 9’s complement representation)Biquinary has two ranges 01… and 10…

one-bit error produces invalid codeword

Page 18: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.
Page 19: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Number representation inside computer

Integers – represented in 2’s complement Single precision – 32 bits

Largest positive integer is 231-1 =

2,147,483,647

Smallest negative integer is -231 =

2,147,483,648

Page 20: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Number representation inside computer

Floating point Scientific notation

0.0043271 = 0.4327110-2

normalized number

The digit after the decimal point is 0

Normalized notation maximizes the use of significant digits.

Page 21: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Floating point numbers

Floating point

N = (-1)S m rE

S = 0 positive

S = 1 negative

m normalized fraction for radix r = 2

As MSB digit is always 1, no need to explicitly store it

Called hidden bit gives one extra bit of precision

S E m

Page 22: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Floating point formats• IEEE format: (-1)S(1.m)2E-127

• DEC format: (-1)S(0.m)2E-128

Page 23: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Floating point formats• Different manufacturers used different

representations

Page 24: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Mechanical encoder with sequentialsector numbering

At boundaries can get a code differentthan either adjacent sector

Page 25: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Gray code:

Page 26: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Gray code algorithm:input: (binary) output: (Gray code)

0121 bbbb nn

0121 gggg nn

else,1

,0 1iii

bbg

e.g. n = 3: 6 -> 110 -> 101

n = 4: 10 -> 1010 -> 1111

Alternative algorithm (n bits) If n = 1 use 0->0 and 1->1 If n > 1 first half is Gray code on (n - 1) bits preceded by 0 second half is reversed Gray code on (n - 1) bits preceded by 1

Page 27: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Representing non-numeric data• Code: systematic and preferably standardized way

of using a set of symbols for representing information– Example: BCD (binary coded decimal) for decimal #s– It is a “weighted” code the value of a symbol is a

weighted sum

• Extending number of symbols to include alphabetic characters– EBCDIC (by IBM): Extended BCD Interchange Code– ASCII: American Standard Code for Information

Interchange

Page 28: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Codes

Page 29: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Cyclic codes

A circular shift of any code word produces a valid code word (maybe the same)

Gray code – example of a cyclic code Code words for consecutive numbers differ by

one bit only

Page 30: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Ascii, ebcdic codesState codes, e.g.

Page 31: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Consequences of binary versus one-hot coding:

Page 32: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

n-cubes of codewords:

Hamming distance between x and y is count of positions wherex-bit differs from y-bit

Also equals link count in shortest path from x to y

Page 33: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Gray code is path that visits each vertex exactly once

Page 34: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Error-detecting codes

Problem Solution: parity

concept:choose code words such thatcorruption generates a non-code word

to detect single-bit error, code words must occupynon-adjacent cube vertices

Page 35: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Example: correct one bit errors or detect two-bit errors

Error-correcting codesminimum distance between code words > 1

Two-bit error from0001011

Page 36: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Write minimum distance as 2c + d + 1 bits ==>corrects c-bit errors anddetects (c + d)-bit errors

Example: min distance = 4 = 2(1) + 1 + 1

But also, 4 = 2(0) + 3 + 1

Page 37: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Why?suppose minimum distance is h

c ch - 2c

d = h - 2c -1

pair of closest nodes

maximally distant from left with entering correction zone

2c + d + 1 = 2c + (h - 2c - 1) + 1 = h

Page 38: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Hamming codes: 12 knnumber bit positions 1, 2, 3, ... n from right to leftbit position is a power of 2 => check bit else => information bite.g. (n = 7)

check bits in positions 1, 2, 4information bits in positions 3, 5, 6, 7

Create group for each check bit Express check bit position in binary Group all information bits whose binary position has a one in same place

e.g. (n = 7)check information1 (001) 3 (011), 5 (101), 7 (111)2 (010) 3 (011), 6 (110), 7 (111)4 (100) 5 (101), 6 (110), 7 (111)

Page 39: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Code information packets to maintain even parity in groups

e.g. (n = 7)packet is 1011 => positions 7, 6, 5, 37 6 5 4 3 2 11 0 1 x 1 x x

Consult group memberships to compute check bits

check information1 3, 5, 7 => bit 1 is 1 2 3, 6, 7 => bit 2 is 04 5, 6, 7 => bit 4 is 0

Code word is 1010101

Page 40: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Note:Single bit error corrupts one or more parity groups => minimum distance > 1

Two-bit error in locations x, y corrupts at least one parity group =>minimum distance > 2

Three-bit error (i.e. 1, 4, 5) goes undetected => minimum distance = 3

3 = 2(1) + 0 + 1 = 2(0) + 2 + 1 => can correct 1-bit errors or detect errorsof size 1 or 2.

Page 41: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Pattern generalizes to longer bit strings:• Single bit error corrupts one or more parity groups => minimum distance > 1

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Group 8Group 4Group 2Group 1

• Consider two-bit error in positions x and y. To avoid corrupting all groups:-- avoid group 1: bit 1 (lsb) same for both x and y-- avoid group 2: bit 2 same for both-- avoid group 4: bit 3 same for both, etc.-- forces x = y

• So true two-bit error corrupts at least one parity group => min distance > 2

• Three-bit error (two msb and lsb) goes undetected => minimum distance = 3

• Conclude: process always produces a Hamming code with min distance = 3

Page 42: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Traditional to permute check bits to far rightUsed in memory protection schemes

Page 43: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Add traditional parity bit (for entire word) to obtaincode with minimum distance = 4

Page 44: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Number of check bits grows slowly with information bits

For minimum distance = 4: total bits 2

parity bits 1

information bits

lg 1payload ratio 1

kn

p k

i n p

n p n n

n n

Page 45: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Two-dimensional codes (product codes)

min distance is product ofrow and column distances

for simple parity on each (below)min distance is 4

Page 46: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Scheme for RAID storageCRC is extension of Hamming codeseach disk is separate rowcolumn is disk block (say 512 characters)rows have CRC on row diskcolumns have even parity

Page 47: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Checksum codesmod-256 sum of info bytes becomes checksum bytemod-255 sum used in IP packets

m-hot codes (m out of n codes)each valid codes has m ones in a frame of n bitsmin distance is 2detect all unidirectional errors

bit flips are all 0 to 1 or 1 to 0

Page 48: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

Serial data transmission(simple) transmit clock and sync with data (3 lines)(complex) recover clock and/or sync from data line

Page 49: Arithmetic operations in binary Addition / subtraction 01011 + 111 ? “Method” exatly the same as decimal.

NRZ: clock recovery except during long sequences of zeros or onesNRZ1: nonreturn to zero, invert on one

zero => no change, one => change polarityRZ: clock recovery except during long sequences of zeroDC balanceBipolar return to zero (aka alternate mark inversion: send one as +1 or -1)Manchester: zero => 0 to 1 transition, one => 1 to 0 transition

at interval center

Serial line codes: