+ All Categories
Home > Documents > Lect5 Binary Arithmetic

Lect5 Binary Arithmetic

Date post: 04-Apr-2018
Category:
Upload: haneesha-muddasani
View: 239 times
Download: 0 times
Share this document with a friend

of 17

Transcript
  • 7/29/2019 Lect5 Binary Arithmetic

    1/17

    TA C162

    Lecture 5 Binary Arithmetic

  • 7/29/2019 Lect5 Binary Arithmetic

    2/17

    Todays Agenda

    Binary Arithmetic

    Addition & Subtraction

    Sign Extension

    Representation of Fractions

    Floating Point Next Class IEEE-754 Standard

    Tuesday, January 19, 2010 2Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    3/17

    2s C Addition & Subtractionxampe : + n t notaton11 01011

    3

    0001111 + 3 01110 (14)

    Example 2:14 - 9 ? (in 5 bit notation)14 011109 01001-9 1011114 + -9 00101 5Tuesday, January 19, 2010 3Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    4/17

    Signed Extension Useful to represent a small number withfewer bits

    Example: 6

    00000110 can be represented as0110.

    A ng ea ng s oes not a ect t e va ue o

    a Number. Question

    How about Negative Numbers ??

    Try this out: 13 + (-5) where 13 with 8 bits and-5 with 5 bits

    Tuesday, January 19, 2010 4Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    5/17

    Sign Extension (SEXT)To add two numbers, we must represent them with the samenumber of bits.

    4-bit 8-bit

    Instead, replicate the MS bit i.e. the sign bit:

    1100 (-4) 00001100 (12, not -4)

    - -

    0100 (4) 00000100 (still 4)

    Leading 0s will not affect the value of positive numbers &1100 (-4) 11111100 (still -4)

    Tuesday, January 19, 2010 5Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    6/17

    Overflow operan s are too g, t en sum cannot e representeas an n-bit 2s compliment number.

    + 01001 (9)

    Above result is Incorrect !!!

    10001 (-15)

    We have overflow if:

    Signs of both operands are the same, and Sign of sum is

    . Another test -- easy for hardware:

    Carr into MS bit does not e ual carr out

    Tuesday, January 19, 2010 6Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    7/17

    Example:

    Carry in for MS BitCarry out for MS Bit1 0

    -+ 10111 (-9)

    sign of result is positive

    Look at both carry!!!

    Tuesday, January 19, 2010 7Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    8/17

    Overflow

    Overflow

    Overflow

    negative number is added?

    Tuesday, January 19, 2010 8Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    9/17

    Addition & Subtraction Adding a Number to itself (Multiply by 2)

    X + X = 2X

    4 00100 8 01000 7 00111 14 01110

    -211110 -4 11100

    Equivalent to shifting each bit one position to the left

    Dividing a number by 2

    Equivalent to shifting all the bits right one position

    Tuesday, January 19, 2010 9Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    10/17

    Fractions: Fixed-Point How can we represent fractions?

    negative powers of two -- just like decimal point.

    2-1 = 0.5

    2-2 = 0.25xampe:

    11111110.110

    2- = 0.125

    Tuesday, January 19, 2010 10Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    11/17

    Conversion from Binary to Decimal(Fractions)Convert to decimal: 00101000.101

    ep : a e n eger par an conver o ec ma

    23 + 25 = 40

    Step2: Take fraction part and convert to decimal

    2-1+2-3 = .625

    Step3: Add result of Step1 and Step2

    40.625

    Tuesday, January 19, 2010 11Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    12/17

    Arithmetic on Fractionss comp a ton an su tracton st wor .

    If binary points are aligned 2-1 = 0.5

    2-2 =0.25

    00101000.101 (40.625)

    2-3

    = 0.125

    + 11111110.110 (-1.25)00100111.011 (39.375)

    =-

    .110 = 0.75

    . - . = - .

    Tuesday, January 19, 2010 12Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    13/17

    Precision and Range Precision: Difference between successive values

    for a given data type

    Precision of the number, is determined by the number offractional bits.

    -,to the right of the binary point has a precision of 2-3

    i.e. value of least significant bit

    Range

    The range of numbers

    For example, for 8 bit 2s compliment data type therange is 128.

    Tuesday, January 19, 2010 13Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    14/17

    Representations of very Big and very

    ma um ersHow many Bits required to represent 6.023 x 1023 ?

    How many Bits required to represent 6.626 x 10-34 ?

    Can we represent above numbers by using 32 Bits? IfYES, HOW?

    By Floating Point Representation

    Tuesday, January 19, 2010 14Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    15/17

    Floating-Point Representation Use equivalent of scientific notation: F x 2E

    Need to represent F (fraction), E (exponent), andsign.

    IEEE 754 Floating-Point Standard (32-bits):

    1b 8b 23b

    S exponent fraction

    0exponent,2fraction.0)1(

    254exponent1,2fraction.1)1(126

    127exponent

    ==

    =

    S

    S

    N

    N

    Tuesday, January 19, 2010 15Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    16/17

    Floating Point Example Single-precision IEEE floating point number

    1 01111110 10000000000000000000000

    sign exponent fraction

    Sign is 1number is negative.

    = .

    Fraction is 0.100000000000 = 0.5 (decimal).

    Value = -1.1 x 2(126-127) = -1.1x 2-1 = -0.11

    - . .

    Tuesday, January 19, 2010 16Biju K Raveendran@BITS Pilani.

  • 7/29/2019 Lect5 Binary Arithmetic

    17/17

    Floating Point ExampleRepresent 1/8 (0.125) in IEEE 754 format?

    Binary equivalent of 0.125 is 0.001 or 1.0 x 2-3

    orma ze

    N = (-1)s X 1.fraction X 2exponent-127

    Sign bit = 0 (Number is positive)

    exponent - 127 = -3 i.e. exponent = 124Binary equivalent of 124 = 01111100

    Fraction = 00000000000000000000000

    Final representation of 1/8 in IEEE 754 format is

    Tuesday, January 19, 2010 17Biju K Raveendran@BITS Pilani.


Recommended