04 number representations

Upload: sheger

Post on 05-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 04 Number Representations

    1/24

    Number representations

    We often want to do numerical computations in digital systems so we need to

    understand how to represent and manipulate binary numbers (and, in general, in

    number systems other than decimal).

    ECE124 Digital Circuits and Systems Page 1

  • 7/31/2019 04 Number Representations

    2/24

    Position number representation

    ECE124 Digital Circuits and Systems Page 2

    Lets assume no decimal portions Any number can be written as:

    Here, r is called the base or radix and the a_i are called the coefficients. The

    coefficients are always between 0,1, (r-1).

    Examples:

  • 7/31/2019 04 Number Representations

    3/24

    Different bases

    Some important bases:

    Base-10 (decimal)we are familiar with this system. Digits are 0, 1, 2, , 8, 9.

    Base-2 (binary) Digits are 0, 1.

    Base-8 (octal)Digits are 0, 1, 2, , 7.

    Base-16 (hexadecimal)Digits are 0, 1, 2, , 9, A, B, C, D, E, F.

    In a binary system (base-2), the digits/coefficients are often called bits.

    ECE124 Digital Circuits and Systems Page 3

  • 7/31/2019 04 Number Representations

    4/24

    Fixed point number representations

    If we have fractional numbers (i.e., something after the decimal) we can do effectively

    the same thing.

    We can do this in any base or radix:

    ECE124 Digital Circuits and Systems Page 4

    Example:

  • 7/31/2019 04 Number Representations

    5/24

    Number conversions (base-r to base-10).

    Conversion from base-r to base-10 is easy; Just expand the base-r representation and

    the result is a base-10 result.

    ECE124 Digital Circuits and Systems Page 5

  • 7/31/2019 04 Number Representations

    6/24

    Number conversions from base-10 to base-r (1)

    We do the integer part and the fractional part separately.

    Consider the integer part of a number in base-r and its division by the base-r:

    ECE124 Digital Circuits and Systems Page 6

    Conversion procedure should be clear; we take a decimal number and performsuccessive division by r until the quotient becomes 0.

    The remainders provide the coefficients in the new base.

  • 7/31/2019 04 Number Representations

    7/24

    Number conversions from base-10 to base-r (2)

    Convert (53)10 to base-2.

    ECE124 Digital Circuits and Systems Page 7

    We read the digits from bottom to top, writing them left to right.

    Result is that (53)10 = (110101)2

    The left most bit is the MSB (most-significant bit) and the right most bit is the LSB

    (least significant bit).

  • 7/31/2019 04 Number Representations

    8/24

    Number conversions from base-10 to base-r (3)

    Consider the fractional part of a number in base-r and its multiplication by the base-r:

    ECE124 Digital Circuits and Systems Page 8

    Conversion should be clear; We can take a decimal number and perform successive

    multiplication by r;

    Continue until the fractional part of the multiplication becomes 0.

    The integer parts provide the coefficients in the new base.

  • 7/31/2019 04 Number Representations

    9/24

    Number conversions from base-10 to base-r (4)

    Convert (0.625)10 to base-2.

    ECE124 Digital Circuits and Systems Page 9

    We read the bits top to bottom, and write them after the radix point left-to-right.

    Result is (0.625)10 = (0.101)2

  • 7/31/2019 04 Number Representations

    10/24

    Conversions between binary, octal and hexadecimal numbers

    Nice simple ways to convert between these three number systems, since all are a

    power of 2.

    Binary to Octal simply requires grouping bits into groups of 3-bits and converting.

    Binary to Hexadecimal simply requires grouping bits into groups of 4-bits and

    converting.

    Going the other direction (Octal to Binary or Hexadecimal to Binary) should follow

    Example (100111100101)2 = (4745)8 = (9E5)16

    ECE124 Digital Circuits and Systems Page 10

  • 7/31/2019 04 Number Representations

    11/24

    ECE124 Digital Circuits and Systems Page 11

    Complements

    Complements are used to simplify the subtraction operation when performing digital

    computation.

    Two types of complements for base-r:

    Diminished radix complement also referred to as (r-1)s complement.

    Radix complementalso referred to as rs complement.

    So, for binary systems (base-2), we have 1s complements and 2s complements.

    I am only going to talk about radix complements (2s complements in base-2) since it is

    the most useful.

    Note: Its important to understand that the complement of a number will return the

    original number.

  • 7/31/2019 04 Number Representations

    12/24

    ECE124 Digital Circuits and Systems Page 12

    Radix complements

    Given a number N in base-r having n digits, the rs complement is defined as:

    Examples:

    The 10s complement of (546700)10 is (1000000)-546700 = (453300)10

    The 2s complement of (1011000)2 is (10000000)-1011000 = (0101000)2

    Note: In base-2, the 2s complement of a number is obtained by flipping the bits and

    adding 1.

  • 7/31/2019 04 Number Representations

    13/24

    Addition of unsigned binary numbers

    Binary addition of unsigned numbers is done just like in decimal. We add digits and

    generate carries.

    ECE124 Digital Circuits and Systems Page 13

    Note: We can get a non-zero carry out; if we are using (and are limited to) n-bits, thisbecomes an overflowcondition for unsigned addition.

  • 7/31/2019 04 Number Representations

    14/24

    Subtraction of unsigned binary numbers

    Binary subtraction of unsigned numbers can be done just like in decimal. We subtract

    numbers using borrows when necessary.

    ECE124 Digital Circuits and Systems Page 14

    Note: It sort of doesnt make sense to subtract binary numbers if the minuend issmaller than the subtrahend; A negative result can often be considered anunderflow.

  • 7/31/2019 04 Number Representations

    15/24

    Subtraction of unsigned binary numbers using complements

    The way we perform subtraction by hand is a hassle when done by a digital circuit and

    this is where complements help a

    The subtraction of two numbers M-N in base-r can be done by: Adding the minuend

    M to the rs complement of the subtrahend N. The result mathematically is:

    ECE124 Digital Circuits and Systems Page 15

    Two possible results:

    Case 1: IfM >= N we will get a carry out of 1 (rn) which can be discarded to givethe result.

    Case 2: IfM < N we will not get a carry out. Since the result is rn (N-M), wesimply complement the sum and put a negative sign in front.

  • 7/31/2019 04 Number Representations

    16/24

    ECE124 Digital Circuits and Systems Page 16

    Example of subtraction using rs complement in base-10

    Subtract 72532-3250 assuming

    these are base-10 numbers.

    There is a carry out (equal to 1 in

    this example) which we discard

    and our result is 69282

    Subtract 3250 72532 assuming

    these are base-10 numbers.

    There is no carry out (shown as an

    extra 0) so we need to complement

    the sum and add a negative sign.

    10s complement of 30718 is 69282,

    and therefore our answer is -69282

  • 7/31/2019 04 Number Representations

    17/24

    ECE124 Digital Circuits and Systems Page 17

    Example of subtraction using rs complement in base-2

    Subtract 101-011 (5-3) using

    assuming base-2representations.

    There is a carry out so we need to

    discard the carry out.

    Therefore our answer is (010)2

    Subtract 011-101 (3-5) using assuming

    base-2 representations.

    There is no carry out (shown as extra

    leading 0) so we need to complement

    the result and add a negative sign.

    2s complement of 110 is 010.

    Therefore our answer is(010)2 which is

    -2.

  • 7/31/2019 04 Number Representations

    18/24

    ECE124 Digital Circuits and Systems Page 18

    Signed numbers

    We need to consider methods to represent signed numbers in addition to unsigned

    numbers.

    In a computer, signed or unsigned numbers are both represented as strings of bits;

    given a string of bits it is up to us to interpret the number.

    If a number is signed, the left-most bit (the most-significant bit) tells us the sign:

    0 means a +ve number.

    1 means ave number.

    E.g., 01001 means +9 in either its signed or unsigned representation given only 5

    bits to represent a number.

    E.g., 11001 means +25 in its unsigned representation; some other negative number

    in its unsigned representation given only 5 bits to represent a number.

  • 7/31/2019 04 Number Representations

    19/24

    ECE124 Digital Circuits and Systems Page 19

    Signed Number Representations (1)

    Only 1 way to represent a +ve number, but 3 ways to represent its negative version

    in a binary number system:

    Signed magnitude.

    Signed 1s-complement (we will not talk about this further).

    Signed 2s-complement.

    E.g., consider +9 represented with 8 bits: +9 is (00001001).

    E.g., consider -9 represented with 8 bits:

    Signed magnitude: -9 is (10001001).

    Signed 1s-complement: -9 is (11110110).

    Signed 2s-complement: -9 is (11110111).

  • 7/31/2019 04 Number Representations

    20/24

    ECE124 Digital Circuits and Systems Page 20

    Signed Number Representations (2)

    Signed magnitude representations are obtained by flipping the leading bit of the +ve

    version.

    Signed 2s complement is obtained by flipping all the bits and adding 1

    i.e., taking the 2s complement of the +ve version.

  • 7/31/2019 04 Number Representations

    21/24

    ECE124 Digital Circuits and Systems Page 21

    Different schemes to represent numbers assuming 4 available bits.

    Note the multiple representations of 0 in signed-magnitude and 1s complement.

    Note: we get an extra digit in 2s complement.

  • 7/31/2019 04 Number Representations

    22/24

    ECE124 Digital Circuits and Systems Page 22

    Addition of signed binary numbers using 2s complement

    Signed arithmetic is easy with 2s complement numbers. We just perform the addition

    and ignore the carry out.

  • 7/31/2019 04 Number Representations

    23/24

    ECE124 Digital Circuits and Systems Page 23

    Subtraction of signed binary numbers using 2s complement

    Things are easy using 2s complement representation forve numbes. We take the 2s

    complement of the subtrahend and add. Why does this work?

    E.g., Consider (-6)-(-13) = +7.

    Ignoring the carry out (as we do for addition) we get the correct result of +7.

  • 7/31/2019 04 Number Representations

    24/24

    ECE124 Digital Circuits and Systems Page 24

    Illustration of 2s complement numbers and why they are good for signed binary arithmetic

    Consider that we only have 4-bits in which to represent a signed number. We can

    represent the numbers -8 to +7 (16 different values).

    We can visualize the numbers on a ring.

    Arithmetic operations are modulo.

    When we perform

    addition/subtraction, we are simply

    moving around the ring

    1100

    0000

    1011

    10100110

    0100

    0011

    0010

    1000

    0001

    0101

    01111001

    1101

    1110

    1111

    0+1

    +6

    +5

    +4

    +3

    +2

    -3

    -4

    -5

    -7

    -8+7

    -6

    -2

    -1

    With 2s complement numbers, we get only one representation of zero. Hence, interms if the circuit, 2s complement is nice since we dont need to worry about

    jumping over and extra 0 when performing addition or subtraction.