COMP 1402 Winter 2008 Tutorial #2 Overview of Tutorial #2 Number - - PDF document

comp 1402
SMART_READER_LITE
LIVE PREVIEW

COMP 1402 Winter 2008 Tutorial #2 Overview of Tutorial #2 Number - - PDF document

COMP 1402 Winter 2008 Tutorial #2 Overview of Tutorial #2 Number representation basics Binary conversions Octal conversions Hexadecimal conversions Signed numbers (signed magnitude, ones and twos complement,


slide-1
SLIDE 1

1

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
slide-2
SLIDE 2

2

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:

dn dn-1 dn-2... d2 d1 d0

  • To calculate the value:

dn*bn + dn-1*bn-1 +...+ d1*b1 + d0*b0

  • Decimals (base 10):

– How do we interpret the number 2510? = 2*103 + 5*102 + 1*101 + 0*100

slide-3
SLIDE 3

3

Bases (2)

Given a number dn dn-1 dn-2... d2 d1 d0

  • In binary (base 2)

value = dn*2n + dn-1*2n-1 +...+ d1*21 + d0*20

  • In octal (base 8)

value = dn*8n + dn-1*8n-1 +...+ d1*81 + d0*80

  • In hexadecimal (base 16)

value = dn*16n + dn-1*16n-1 +...+ d1*161 + d0*160

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

slide-4
SLIDE 4

4

Binary: Most/Least Significant bits

  • Most significant bit (MSB) is the leftmost

bit:

– Ex: 10010100 – 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: 10010100 – 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 = dn*2n + dn-1*2n-1 +...+ d1*21 + d0*20

  • Ex: convert (11010)2 to decimal.

This is equal to 1*24 +1*23 + 0*22 + 1*21 +0*20 16 + 8 + 0 + 2 + 0 = 26 (11010)2 = (26)10

slide-5
SLIDE 5

5

Decimal to binary

  • Converting a number from base 10 to

base 2.

  • Let’s look again at the value of a binary

number: dn*2n + dn-1*2n-1 +...+ d1*21 + d0*20

  • We need to fill in the d0 to dn to build the

binary number as dn dn-1 dn-2... d2 d1 d0

  • 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…
slide-6
SLIDE 6

6

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
  • ver 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

slide-7
SLIDE 7

7

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

slide-8
SLIDE 8

8

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

slide-9
SLIDE 9

9

Octal to decimal

  • Count the digits weighed by exponents of

8, as seen previously:

value = dn*8n + dn-1*8n-1 +...+ d1*81 + d0*80

  • Ex: convert (314)8 to decimal.

= 3*82 + 1*81 + 4*80 = 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 = dn*16n + dn-1*16n-1 +...+ d1*161 + d0*160

  • Ex: convert (4A1C)16 to decimal.

= 4*163 + 10*162 + 1*161 + 12*160 = 4*4096 + 10*256 + 1*16 + 12*1 = 16384 + 2560 + 16 + 12 (4A1C)16 = 18972

slide-10
SLIDE 10

10

Part I of the exercises then correction Signed Numbers

  • Signed Magnitude
  • One’s Complement
  • Two’s Complement
  • Excess-M (Bias)
slide-11
SLIDE 11

11

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: 00001110

  • 14 in signed magnitude is: 10001110

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)

slide-12
SLIDE 12

12

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.

slide-13
SLIDE 13

13

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
  • ne’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

slide-14
SLIDE 14

14

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

slide-15
SLIDE 15

15

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: 100110101 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

slide-16
SLIDE 16

16

Float representation

  • Used to represent fractional numbers

– Overall sign – Mantissa (fraction part) – Exponent – Base

  • In C: 32-bit float (4 bytes)

Sign Exponent Mantissa 1 bit 8 bits 23 bits

Float to decimal

  • Convert 1 00000011 11000000000000000000000 from

float (1-bit sign, 8-bit exponent and 23-bit mantissa) to decimal. Sign: “-” since the bit sign is “1” Exponent: 00000011 to decimal = 3, then subtract 127 to convert from Excess-127 to true value: 3-127 = -124. Mantissa: 11 followed by zeroes. We supply the leading 1 (always). So the number is -(1.11)2 * 2-124 = -1.75 * 2-124

slide-17
SLIDE 17

17

Decimal to float

  • Convert (3.6875)10 to float (1-bit sign, 8-bit exponent and

23-bit mantissa). Sign: 0 (since the number is positive)

  • To figure out the mantissa and the exponent, we must

first convert the decimal to binary. 3 to binary = 11 .6875 to binary: 0.6875 * 2 = 1.375 0.375 * 2 = 0.75 0.75 * 2 = 1.5 0.5 * 2 = 1.0 So 3.6875 in binary is 11.1011 (continued next slide)

Decimal to float (2)

  • Floats must always be in the format 1.x so we must

move the period to have 1.x in our binary number: 11.1011 (we have to shift the period one place to the left). So 11.1011 = 1.11011 * 21 We now have the mantissa: 11011(followed by zeroes) And the exponent is 1, converted to Excess-127 = 128. In binary 128 = 10000000. Answer: 0 10000000 11011000000000000000000

  • Convert back to decimal to verify the answer.
slide-18
SLIDE 18

18

Part II of the exercises then correction