Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus 02 Information theory 02.02 Binary arithmetic • Positional notation • Unsigned integers • Unsigned fixed-point • Signed numbers • Floating point numbers • Base conversions • Binary arithmetic alessandro bogliolo isti information science and technology institute 1 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Positional notation of unsigned integers • The base - b positional representation of an integer number of n digits has the form c c ... c c (1) − − n n 1 2 1 0 • The value of the number is n − + n − + + + c b c b c b c b 1 2 1 0 (2) ... n − n − 1 2 1 0 • n digits encode all integer numbers from 0 to b n -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 /17 1
Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion • From base b to decimal: − − c b n + c b n + + c b + c b 1 2 1 0 ... (2) − − n n 1 2 1 0 • From decimal to base b : digits from c 0 to c n- 1 are obtained as remainders of subsequent divisions by b of the digital number n − + n − + + + = c b c b ... c b c b 1 2 1 0 (3) n − n − 1 2 1 0 = n − + n − + + + ( c b c b ... c b ) b c 2 3 0 n − n − 1 2 1 0 alessandro bogliolo isti information science and technology institute 3 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion example (29) (10) =(11101) (2) (11001) (2) =(25) (10) position weight digit 29 1 1 29=14*2+ 1 0 1 1 + 29 14 0 0 14=7*2+ 0 1 2 0 + 14 7 0 1 7=3*2+ 1 2 4 0 + 7 3 1 3 8 8 + 1 3=1*2+ 1 3 1 1 16 16 4 1=0*2+ 1 1 1 0 25 alessandro bogliolo isti information science and technology institute 4 /17 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 rational number of n+m digits has the form c c ... c c . c ... c (4) n − n − − − m 1 2 1 0 1 • The value of the number is − − − − c b n + c b n + ... + c b + c b + c b + ... + c b m 1 2 1 0 1 (5) − − − − n n m 1 2 1 0 1 • n+m digits encode all rational numbers of the form Num (6) m 2 with ≤ ≤ n + m − Num 0 2 1 alessandro bogliolo isti information science and technology institute 5 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion • From base b to decimal: − − − − n + n + + + + + + m c b c b ... c b c b c b ... c b 1 2 1 0 1 (5) n − n − − − m 1 2 1 0 1 • 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 = X int . X frac , convert the integer part X int as outlined before, then convert the fractional part X frac obtaining each digit as the integer part of the result of subsequent multiplications by b : − + − + − m = + − + − m − ( c b c b ... c b ) b c c b ... c b 1 2 1 1 (7) − − − m − − − m 1 2 1 2 alessandro bogliolo isti information science and technology institute 6 /17 3
Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Base conversion example (29.375) (10) =(11101.011) (2) 29 0.375 *2 = 0.75 = 0 1 29=14*2+ 1 + 0.75 29 14 0 14=7*2+ 0 = 1 0.75 *2 = 1.5 + 0.5 14 7 1 7=3*2+ 1 = 1 7 3 0.5 *2 = 1 + 0 1 3=1*2+ 1 3 1 1=0*2+ 1 1 1 0 alessandro bogliolo isti information science and technology institute 7 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Binary arithmetics • 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 multiplication: 1 1 1 0 1 1 1 0 0 1 2 0 0 0 1 1 1 1 1 5 1 0 1 1 1 0 0 6 0 1 1 0 0 - 2 - 1 8 0 1 1 0 0 - - 1 1 1 0 0 - - - 1 0 1 1 0 1 0 0 alessandro bogliolo isti information science and technology institute 8 /17 4
Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Signed numbers • Sign 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 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus 2’s complement numbers • The 2 ’ s complement representation of a negative number (say, - v ) is obtained from the representation of v, by complementing each bit (including the sign bit) and adding 1 - v � v ’ +1 • The resulting binary configuration corresponds to the unsigned number 2 n - v (i.e. , the complement of v to 2 n ), where n is the total number of digits, including the sign 2 n bit. 2 n -v 0 v -v negative positive alessandro bogliolo isti information science and technology institute 10 /17 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+( 2 n -b)= 2 n -(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 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point numbers s • 0. M • b se • 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 /17 6
Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point encoding s • 0. M • b se • 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 Take as mantissa ( M ) the most significant m bits 3. (starting from the first 1) of the fixed-point encoding Set se =1 if the number is lower than 0.5, se =0 4. otherwise Assign the exponent E with the binary 5. representation of the number of shifts computed at step 2 Set the sign bit s as usual 6. alessandro bogliolo isti information science and technology institute 13 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point encoding s • 0. M • b se • E Example: m =4, e =3 s M se E (40) (10) 5 4 3 2 1 0 -1 0 1 0 1 0 0 1 1 0 40 1 0 1 0 0 0 . 0 6 positions s se M E 5 4 3 2 1 0 -1 (-19) (10) 1 1 0 0 1 0 1 0 1 -18 0 1 0 0 1 1 . 0 5 positions s se M E -1 -2 -3 -4 -5 -6 -7 -8 (0.1) (10) 0 1 1 0 0 1 0 1 1 0.09375 . 0 0 0 1 1 0 0 1 ... 3 positions alessandro bogliolo isti information science and technology institute 14 /17 7
Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point arithmetic se A E A = s . M = se B E B s . M 0 2 A B 0 2 A A B B = + = se E + se E C A B s . M s . M 0 2 A A 0 2 B B A A B B se E 2 B B = + se E s . M s . M 0 0 2 A A A A B B se E A A 2 ( ) = + − ( se E − se E ) se E s . M s . M 0 0 2 A A B B 2 A A A A B B C = A ∗ B = s . M se E ∗ s . M se E 0 2 A A 0 2 B B A A B B ( ) = s . M ∗ s . M se E ∗ se E 0 0 2 A A 2 B B A A B B ( ) ( ) + = s . M ∗ s . M se E se E 0 0 2 A A B B A A B B alessandro bogliolo isti information science and technology institute 15 /17 Computer Architecture applied computer science 02.02 Binary arithmetic urbino worldwide campus Floating point arithmetic s • 0. M • b se • 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.25 = +0.10101*2 +3 C = A+B = 5 = +0.101*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 /17 8
Recommend
More recommend