COMP 1402 - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

COMP 1402

Description:

... (3.6875)10 to float (1-bit sign, 8-bit exponent and 23-bit mantissa) ... To figure out the mantissa and the exponent, we must first convert the decimal to binary. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 36
Provided by: scsCar
Category:
Tags: comp | mantissa

less

Transcript and Presenter's Notes

Title: COMP 1402


1
COMP 1402
  • Winter 2008
  • Tutorial 2

2
Overview of Tutorial 2
  • Number representation basics
  • Binary conversions
  • Octal conversions
  • Hexadecimal conversions
  • Signed numbers (signed magnitude, ones and twos
    complement, Excess-M)
  • Float conversions

3
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

4
Bases
  • Arbitrary base b
  • dn dn-1 dn-2... d2 d1 d0
  • To calculate the value
  • dnbn dn-1bn-1 ... d1b1 d0b0
  • Decimals (base 10)
  • How do we interpret the number 2510?
  • 2103 5102 1101 0100

5
Bases (2)
  • Given a number dn dn-1 dn-2... d2 d1 d0
  • In binary (base 2)
  • value dn2n dn-12n-1 ... d121
    d020
  • In octal (base 8)
  • value dn8n dn-18n-1 ... d181
    d080
  • In hexadecimal (base 16)
  • value dn16n dn-116n-1 ... d1161
    d0160

6
Fractions in bases
  • Arbitrary base fractions
  • 0. d-1 d-2 d-3
  • value d-1b-1 d-2b-2 d-3b-3
  • In decimal (base 10)
  • 0.512 is equal to 510-1 110-2 210-3
  • In binary (base 2)
  • 0.1011 is equal to 12-1 02-2 12-3
    12-4
  • 10.5 00.25 10.125 10.0625 0.6875

7
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 well see later).
  • Least significant bit (LSB) is the rightmost bit
  • Ex 10010100
  • Has the least value of all the bits (0 or 1).

8
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 dn2n dn-12n-1 ... d121 d020
  • Ex convert (11010)2 to decimal.
  • This is equal to 124 123 022 121
    020
  • 16 8 0 2 0
    26
  • (11010)2 (26)10

9
Decimal to binary
  • Converting a number from base 10 to base 2.
  • Lets look again at the value of a binary number
    dn2n dn-12n-1 ... d121 d020
  • 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)

10
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
  • Lets try an example

11
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

12
Decimal fraction to binary
  • Convert both sides of the period separately.
    Weve 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

13
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.
  • Well convert each digit separately
  • Octal 7 2 1
    3
  • Binary 111 010 001 011
  • Therefore (7213)8 (111010001011)2

14
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

15
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

16
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

17
Octal to decimal
  • Count the digits weighed by exponents of 8, as
    seen previously
  • value dn8n dn-18n-1 ... d181
    d080
  • Ex convert (314)8 to decimal.
  • 382 181 480
  • 364 18 41
  • 192 8 4
  • (314)8 (204)10

18
Hexadecimal to decimal
  • Count the digits weighed by exponents of 16, as
    seen previously
  • value dn16n dn-116n-1 ... d1161
    d0160
  • Ex convert (4A1C)16 to decimal.
  • 4163 10162 1161 12160
  • 44096 10256 116
    121
  • 16384 2560 16 12
  • (4A1C)16 18972

19
Part I of the exercisesthen correction
20
Signed Numbers
  • Signed Magnitude
  • Ones Complement
  • Twos Complement
  • Excess-M (Bias)

21
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 weve seen before.
  • What happens with numbers smaller than -127?
  • overflow
  • Example (-14)10
  • 14 in binary is 00001110
  • -14 in signed magnitude is 10001110

22
Ones complement
  • MSB is the sign bit
  • 1 is negative
  • 0 is positive
  • Rules to change sign
  • Flip all the bits (change 0s to 1s and 1s to
    0s)
  • Two zeroes 00000000 and 11111111
  • Harder to know the value of a negative number
    (have to be complemented first)

23
Ones 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 ones complement
  • 44 in binary is 00101100
  • -44 in ones complement is 11010011
  • Example (38)10 in ones complement
  • 38 in binary is
    00100110
  • 38 in ones complement is 00100110
    (same)

24
Ones Complement (binary to decimal)
  • If the MSB is 1, we must flip every bit before
    converting to decimal.
  • Ex convert (10011100)2 from ones complement to
    decimal.
  • Flip the bits since M.S.B. is a 1 01100011
  • Then find the decimal value
  • 643221 99 and we add the sign (-99)10
  • If the number is positive (MSB is 0) we convert
    to decimal without any other changes.

25
Twos complement
  • MSB is the sign bit
  • 1 is negative
  • 0 is positive
  • Rules to change sign
  • Take ones complement, then add 1.
  • Only one zero 00000000
  • Have to take the twos complement of negatives in
    order to know the value.

26
Twos Complement (decimal to binary)
  • Convert the decimal to binary, then to ones
    complement, then add 1.
  • Ex convert (-57)10 to twos complement.
  • 57 in binary is 00111001
  • in ones complement 11000110
  • add 1
    1
  • in twos complement 11000111

27
Twos Complement (binary to decimal)
  • If the MSB is 1, take ones complement (flip
    every bit) then add 1.
  • Ex convert (11001100)2 from twos complement to
    decimal.
  • Take ones complement 00110011
  • add 1
    1

  • 00110100
  • and convert to decimal 32164 52 so the
    answer is (-52)10

28
Excess-M (Bias)
  • 8-bit version has 256 values (-127 to 128).
  • Used to store the exponent in the float rep.
  • For floating point, well 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

29
Addition in Ones complement
  • First convert both decimals in ones complement
    then add. If there is a carry, add it to the
    right.
  • Ex add (-15)10 and (69)10 in ones complement.
  • -15 in ones complement 11110000
  • 69 in ones complement 01000101
  • add
    100110101
  • add carry to right
    1
  • answer
    00110110 or 54

30
Addition in Twos complement
  • First convert both decimals in twos complement,
    then add. If there is a carry, discard it.
  • Ex add (-22)10 and (19)10 in twos complement
  • -22 in twos complement 11101010
  • 19 in twos complement 00010011
  • add
    11111101
  • Since MSB is 1, take twos complement to get
    the answer 00000011 or
    -3

31
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
32
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

33
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)

34
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.

35
Part II of the exercises then correction
Write a Comment
User Comments (0)
About PowerShow.com