2's complement

Discussion in 'OT Technology' started by mrburner, Oct 26, 2003.

  1. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    does anyone have any sample code (c++) or can anyone teach me how to do this? part of this project is basically to read an input file (that the user specifies) and output it in binary using 2's complement...basically

    thanks
     
  2. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Where you stuck at?
     
  3. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    just the entire thing...he gave us this ridiculouse assignment and nobody has learned 2's complement yet...i get the general idea but i have no idea how to code it...i did a few searches but couldn't come up with any like example c++ code...that would help me the most
     
  4. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    You have to convert an integer input to a string output of 1's and 0's? You should just be able to use shift and bitwise operations to achieve that:

    while ($input) {
    $bit = $input >> 1;
    if ($bit) print "1";
    else print "0";
    }

    That's just pseudo code of how to do it. Then obviously to convert to two's complement just invert and add 1 to the result.
     
  5. TenzoR

    TenzoR She is hot hot hot

    Joined:
    Nov 4, 2001
    Messages:
    4,162
    Likes Received:
    0
    Location:
    Toronto, Ontario
    do it in assembly :o
     

Share This Page