<<

Architecture applied computer science 02.02 Binary urbino worldwide campus

02 Information theory

02.02 Binary arithmetic

• Positional • Unsigned • Unsigned fixed-point • Signed • Floating point numbers • Base conversions • Binary arithmetic

alessandro bogliolo isti information science and technology institute 1/16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Positional notation of unsigned integers • The base -b positional representation of an of n digits has the form

cn−1cn−2... c1c0 (1) • The value of the number is n−1 n−2 1 0 (2) cn−1b + cn−2b +... + c1b + c0b • n digits encode all integer numbers from 0 to bn-1 Example: b=2, n=5 10011=1*16+1*2+1*1=19 11111=31=2 5-1

alessandro bogliolo isti information science and technology institute 2/16

1 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion

• From base b to : n−1 n−2 1 0 (2) cn−1b + cn−2b +... + c1b + c0b • From decimal to base b:

digits from c0 to cn-1 are obtained as remainders of subsequent divisions by b of the digital number

n−1 n−2 1 0 (3) cn−1b + cn−2b + ... + c1b + c0b = n−2 n−3 0 = c( n−1b + cn−2b + ... + c1b b) + c0

alessandro bogliolo isti information science and technology institute 3/16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion example

(29 )(10) =( 11101 )(2) (11001 )(2) =( 25 )(10)

29 position digit weight 29 14 1 29=14*2+ 1 0 1 1 1 + 14 7 0 14=7*2+ 0 1 0 2 0 + 0 7 3 1 7=3*2+ 1 2 4 0 + 1 3 1 1 3=1*2+ 1 3 8 8 + 4 1 16 16 1 0 1 1=0*2+ 1 25

alessandro bogliolo isti information science and technology institute 4/16

2 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Fixed-point notation of unsigned rational numbers • The base -b positional representation of a of n+m digits has the form

cn−1cn−2 ... c1c0 c. −1... c−m (4) • The value of the number is n−1 n−2 1 0 −1 −m (5) cn−1b + cn−2b + ... + c1b + c0b + c−1b + ... + c−mb • n+m digits encode all rational numbers of the

form Num (6) 2m with 0 ≤ Num ≤ 2n+m −1

alessandro bogliolo isti information science and technology institute 5/16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion

• From base b to decimal: n−1 n−2 1 0 −1 −m cn−1b + cn−2b + ... + c1b + c0b + c−1b + ... + c−mb (5) • From decimal to base b: – Solution 1: Given a digital number X=Num /2 m, convert Num and shift the decimal point m positions left

– Solution 2: Given a digital number X=Xint .Xfrac , convert the integer part Xint as outlined before, then convert the fractional part Xfrac obtaining each digit as the integer part of the result of subsequent by b: −1 −2 −m −1 −m−1 (7) c( −1b + c−2b ... + c−mb b) = c−1 + c−2b ... + c−mb

alessandro bogliolo isti information science and technology institute 6/16

3 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion example

(29 .375 )(10) =( 11101 .011 )(2)

29 29 14 1 29=14*2+ 1 0.375 *2 = 0.75 = 0 + 0.75 14 7 0 14=7*2+ 0 0.75 *2 = 1.5 = 1 + 0.5 7 3 1 7=3*2+ 1 0.5 *2 = 1 = 1 + 0 3 1 1 3=1*2+ 1 1 0 1 1=0*2+ 1

alessandro bogliolo isti information science and technology institute 7/16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Binary

• Binary addition: 1 1 1 1 0 1 1 5 0 1 1 1 1 0 0 1 6 0 0 1 1 0 1 1 10 2 1 1 0 1 0 1 • Binary :

1 0 1 1 2 1 1 0 0 0 0 0 1 3 1 1 0 1 1 0 1 3 6 1 1 0 0 1 2 - 0 0 0 0 - 1 5 6 1 1 0 0 - - 1 1 0 0 - - - 1 0 0 1 1 1 0 0

alessandro bogliolo isti information science and technology institute 8/16

4 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Signed numbers

bit: 0=positive, 1=negative

(3) (10) = 0 0011; (-3) (10) = 1 0011

• 1’s complement

(3) (10) = 0 0011; (-3) (10) = 1 1100 = ( 0 0011) ’

• 2’s complement

(3) (10) = 0 0011; (-3) (10) = 1 1101 = ( 0 0011) ’+1

alessandro bogliolo isti information science and technology institute 9/16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus 2’s complement numbers • The 2 ’s complement representation of a (say, -v) is obtained from the representation of v, by complementing each bit (including the sign bit) and adding 1 -vv’+1 • The resulting binary configuration corresponds to the unsigned number 2n-v (i.e. , the complement of v to 2n), n where is the total numbern of digits, including the sign bit. 2 2n-v 0 -v v negative positive

alessandro bogliolo isti information science and technology institute 10 /16

5 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus 2’s complement arithmetic

• 2’s complement numbers can be added by using the conventional rules of binary addition a+(-b)  a+( 2n-b)= 2n-(b-a) -(b-a)=a-b • Example:

(3) (10) -(5) (10) (00011) (2) -(00101) (2) = (00011) (2) +(-00101) (2) (00011) (2c) +(11011) (2c) = (11110) (2c) (-00010) (2) (-2) (10)

alessandro bogliolo isti information science and technology institute 11 /16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point numbers s•0. M•bse •E • s = sign (1 bit) • M = Mantissa ( m bits ) • b = base (0 bits) • se = exponent sign (1 bit) • E = exponent ( e bits)

Encoding floating point numbers requires words of n digits, with n= 1+m+ 1+e

alessandro bogliolo isti information science and technology institute 12 /16

6 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point encoding s•0. M•bse •E

1. Start from a fixed-point binary encoding using a sufficient number of bits 2. Evaluate the number of shifts required to bring the most significant 1 in position –1 3. Take as mantissa ( M) the most significant m bits (starting from the first 1) of the fixed-point encoding 4. Set se =1 if the number is lower than 0.5, se =0 otherwise 5. Assign the exponent E with the binary representation of the number of shifts computed at step 2 6. Set the sign bit s as usual

alessandro bogliolo isti information science and technology institute 13 /16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point encoding s•0. M•bse •E Example: m=4, e=3 s M se E (40) 543210 -1 0 1 0 1 0 0 1 1 0 40 (10) 1 0 1 0 0 0 . 0

6 positions s M se E (-19) 543210 -1 1 1 0 0 1 0 1 0 1 -18 (10) 0 1 0 0 1 1 . 0

5 positions s se -1 -2 -3 -4 -5 -6 -7 -8 M E 0 1 1 0 0 1 0 1 1 (0.1) (10) . 0 0 0 1 1 0 0 1 ... 0.09375

3 positions

alessandro bogliolo isti information science and technology institute 14 /16

7 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point arithmetic

se AEA se B EB A = sA 0.M A 2 B = sB 0.M B 2

se AEA se B EB C = A + B = sA 0.M A 2 + sB 0.M B 2  2se B EB    se AEA =  sA 0.M A + sB 0.M B se E 2  2 A A 

− se( AEA −se B EB ) se AEA = (sA 0.M A + sB 0.M B 2 )2

se AEA se B EB C = A∗ B = sA 0.M A 2 ∗ sB 0.M B 2

se AEA se B EB = (sA 0.M A ∗ sB 0.M B )2 ∗2

(se AEA +se B EB ) = (sA 0.M A ∗ sB 0.M B )2

alessandro bogliolo isti information science and technology institute 15 /16

Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point arithmetic s•0. M•bse •E

A = 3.5 = +0.111*2 +2 A = 3.5 = +0.111*2 +2 B = 1.5 = +0.110*2 +1 B = 1.5 = +0.110*2 +1 C = A+B = 5 = +0.101*2 +3 C = A*B = 5.25 = +0.101 01 *2 +3

M E A . 1 1 1 2 M E B . 1 1 0 1 A . 1 1 1 2 B . 1 1 0 1 A . 1 1 1 2 B . 0 1 1 2 C . 1 0 1 0 1 3

C 1 . 0 1 0 2 C . 1 0 1 3 5

C . 1 0 1 3

alessandro bogliolo isti information science and technology institute 16 /16

8