COMP 1402 Winter 2008 Tutorial #2 Overview of Tutorial #2 • Number representation basics • Binary conversions • Octal conversions • Hexadecimal conversions • Signed numbers (signed magnitude, one’s and two’s complement, Excess-M) • Float conversions 1
Number representation basics • In binary each digit (or bit) has 2 possible values: 0 or 1. Ex: (10010101) 2 • In octal each digit has 8 possible values: 0, 1, 2… 7. Ex: (4127) 8 • In hexadecimal (or hex for short) each digit has 16 possible values: 0, 1, 2… 9, then A, B, C, D, E and F (representing 10, 11, 12, 13 ,14 and 15 in decimal respectively). Ex: (1AF6) 16 or 1AF6h Bases • Arbitrary base b : d n d n-1 d n-2 ... d 2 d 1 d 0 • To calculate the value: d n * b n + d n-1 * b n-1 +...+ d 1 * b 1 + d 0 * b 0 • Decimals (base 10): – How do we interpret the number 2510? = 2* 10 3 + 5* 10 2 + 1* 10 1 + 0* 10 0 2
Bases (2) Given a number d n d n-1 d n-2 ... d 2 d 1 d 0 • In binary (base 2) value = d n * 2 n + d n-1 * 2 n-1 +...+ d 1 * 2 1 + d 0 * 2 0 • In octal (base 8) value = d n * 8 n + d n-1 * 8 n-1 +...+ d 1 * 8 1 + d 0 * 8 0 • In hexadecimal (base 16) value = d n * 16 n + d n-1 * 16 n-1 +...+ d 1 * 16 1 + d 0 * 16 0 Fractions in bases • Arbitrary base fractions: 0. d -1 d -2 d -3 value = d -1 *b -1 + d -2 *b -2 + d -3 *b -3 • In decimal (base 10): 0.512 is equal to 5*10 -1 + 1*10 -2 + 2*10 -3 • In binary (base 2): 0.1011 is equal to 1*2 -1 + 0*2 -2 + 1*2 -3 + 1*2 -4 = 1*0.5 + 0*0.25 + 1*0.125 + 1*0.0625 = 0.6875 3
Binary: Most/Least Significant bits • Most significant bit (MSB) is the leftmost bit: – Ex: 1 0010100 – It is the bit of highest value (128 in the example above) but can also be used as the sign of the number (as we’ll see later). • Least significant bit (LSB) is the rightmost bit: – Ex: 1001010 0 – Has the least value of all the bits (0 or 1). Binary to decimal • To convert binary to decimal we must add the digits weighed by exponents of 2 used in the binary number as seen previously. value = d n * 2 n + d n-1 * 2 n-1 +...+ d 1 * 2 1 + d 0 * 2 0 • Ex: convert (11010) 2 to decimal. This is equal to 1* 2 4 +1* 2 3 + 0* 2 2 + 1* 2 1 +0* 2 0 16 + 8 + 0 + 2 + 0 = 26 (11010) 2 = (26) 10 4
Decimal to binary • Converting a number from base 10 to base 2. • Let’s look again at the value of a binary number: d n * 2 n + d n-1 * 2 n-1 +...+ d 1 * 2 1 + d 0 * 2 0 • We need to fill in the d 0 to d n to build the binary number as d n d n-1 d n-2 ... d 2 d 1 d 0 • Different ways to solve this – Start with the largest power of 2 in the decimal number, then move down (slow) – Algorithm using the “mod” approach (easier) Decimal to binary (2) Decimal to binary algorithm: Q = decimal number While Q is not equal to 0 do the following: Binary digit = Q mod 2 Q = Q / 2 (quotient) End While • Let’s try an example… 5
Decimal to binary (3) Example: convert (134) 10 to binary. 134 mod 2 = 0 67 mod 2 = 1 33 mod 2 = 1 16 mod 2 = 0 8 mod 2 = 0 4 mod 2 = 0 2 mod 2 = 0 1 mod 2 = 1 We then read the numbers from bottom up: 10000110 So (134) 10 = (10000110) 2 Decimal fraction to binary • Convert both sides of the period separately. We’ve seen how to do the left side, now the fraction side. • Instead of mod, we multiply by two (*2). When we go over 1.0, subtract 1 for next round. • Ex: Convert (0.21875) 10 to binary. 0.21875 * 2 = 0 .4375 0.4375 * 2 = 0 .875 0.875 * 2 = 1 .75 0.75 * 2 = 1 .5 0.5 * 2 = 1 .0 (stop at 1.0) Then we read the numbers top to bottom: 00111 Therefore (0.21875) 10 = 0. 00111 6
Octal to binary • Since every octal digit can take 8 values we can convert each digit to binary using 3 bits. • Ex: convert (7213) 8 to binary. We’ll convert each digit separately: Octal: 7 2 1 3 Binary: 111 010 001 011 Therefore: (7213) 8 = (111010001011) 2 Binary to octal • Group the binary number into groups of 3 bits, starting from the right, then convert each group into their octal value. • Ex: convert (11010110011101) 2 to octal. 11 010 110 011 101 3 2 6 3 5 Therefore: (11010110011101) 2 = (32635) 8 7
Hexadecimal to binary • Since every digit of hex has 16 possible values, we represent each digit using 4 binary bits. • Ex: convert (5DE9) 16 to binary. convert each digit into four binary bits: 5 D E 9 0101 1101 1110 1001 Therefore (5DE9) 16 = (0101110111101001) 2 Binary to hexadecimal • Group the binary number into groups of 4 bits, starting from the right, then convert each group into their hex value. • Ex: (10010010111110111) 2 to hex. 1 0010 0101 1111 0111 1 2 5 F 7 Therefore (10010010111110111) 2 = (125F7) 16 8
Octal to decimal • Count the digits weighed by exponents of 8, as seen previously: value = d n * 8 n + d n-1 * 8 n-1 +...+ d 1 * 8 1 + d 0 * 8 0 • Ex: convert (314) 8 to decimal. = 3*8 2 + 1*8 1 + 4*8 0 = 3*64 + 1*8 + 4*1 = 192 + 8 + 4 (314) 8 = (204) 10 Hexadecimal to decimal • Count the digits weighed by exponents of 16, as seen previously: value = d n * 16 n + d n-1 * 16 n-1 +...+ d 1 * 16 1 + d 0 * 16 0 • Ex: convert (4A1C) 16 to decimal. = 4*16 3 + 10*16 2 + 1*16 1 + 12*16 0 = 4*4096 + 10*256 + 1*16 + 12*1 = 16384 + 2560 + 16 + 12 (4A1C) 16 = 18972 9
Part I of the exercises then correction Signed Numbers • Signed Magnitude • One’s Complement • Two’s Complement • Excess-M (Bias) 10
Signed Magnitude • MSB is 1 if the number is negative (-), 0 if the number is positive (+). • The rest of the number is converted to binary like we’ve seen before. • What happens with numbers smaller than -127? – overflow • Example: (-14) 10 : 14 in binary is: 0 0001110 -14 in signed magnitude is: 1 0001110 One’s complement • MSB is the sign bit – 1 is negative – 0 is positive • Rules to change sign: – Flip all the bits (change 0’s to 1’s and 1’s to 0’s) • Two zeroes: 00000000 and 11111111 • Harder to know the value of a negative number (have to be complemented first) 11
One’s Complement (decimal to binary) • First, convert the decimal to binary • If the number is negative, flip every bit (0 become 1, 1 becomes 0). Positive numbers are converted to binary without change. • Example: (-44) 10 in one’s complement 44 in binary is: 00101100 -44 in one’s complement is: 11010011 • Example: (38) 10 in one’s complement 38 in binary is: 00100110 38 in one’s complement is: 00100110 (same) One’s Complement (binary to decimal) • If the MSB is “1”, we must flip every bit before converting to decimal. • Ex: convert (10011100) 2 from one’s complement to decimal. Flip the bits since M.S.B. is a “1”: 01100011 Then find the decimal value: 64+32+2+1 = 99 and we add the sign= (-99) 10 • If the number is positive (MSB is “0”) we convert to decimal without any other changes. 12
Two’s complement • MSB is the sign bit – 1 is negative – 0 is positive • Rules to change sign: – Take one’s complement, then add 1. • Only one zero: 00000000 • Have to take the two’s complement of negatives in order to know the value. Two’s Complement (decimal to binary) • Convert the decimal to binary, then to one’s complement, then add 1. • Ex: convert (-57) 10 to two’s complement. 57 in binary is: 00111001 in one’s complement: 11000110 add 1: + 1 in two’s complement: 11000111 13
Two’s Complement (binary to decimal) • If the MSB is “1”, take one’s complement (flip every bit) then add 1. • Ex: convert (11001100) 2 from two’s complement to decimal. Take one’s complement : 00110011 add 1: + 1 00110100 and convert to decimal = 32+16+4 = 52 so the answer is (-52) 10 Excess-M (Bias) • 8-bit version has 256 values (-127 to +128). • Used to store the exponent in the float rep. • For floating point, we’ll use Excess-127. • Ex: Convert 32 to Excess-127 = 32 + 127 = 159 or 10011111 • To convert Excess-127 back to decimal: convert to decimal then subtract 127. • Ex: Convert 11001010 in Excess-127 to decimal 11001110 = 206 206 – 127 = (79) 10 14
Addition in One’s complement • First convert both decimals in one’s complement then add. If there is a carry, add it to the right. • Ex: add (-15) 10 and (+69) 10 in one’s complement. -15 in one’s complement: 11110000 +69 in one’s complement: 01000101 add: 1 00110101 add carry to right: 1 answer: 00110110 or +54 Addition in Two’s complement • First convert both decimals in two’s complement, then add. If there is a carry, discard it. • Ex: add (-22) 10 and (19) 10 in two’s complement -22 in two’s complement: 11101010 19 in two’s complement: 00010011 add: 11111101 Since MSB is “1”, take two’s complement to get the answer: 00000011 or -3 15
Recommend
More recommend