Question about Two's Complement

This is a discussion on Question about Two's Complement within the Tech Board forums, part of the Community Boards category; I am having a little trouble understand two's complement. I kind of get the general idea. What I don't get ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Question about Two's Complement

    I am having a little trouble understand two's complement. I kind of get the general idea. What I don't get is why an overflow occurs here? 1100 1000 which is 100+100 does in fact equal 200.... Can someone please explain this to me?


    Thanks.... (The problem below was copied from my notes from class)

    0110 0100 = +100
    + 0110 0100 = +(+100)
    -------------------
    1100 1000 = -56

    WRONG! Carry into MSB is 1 and carry out of MSB is 0. Overflow condition occurs!

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Also,
    Why does: (- 40 * 144) mod 71 =62???!

    When I calculate it, I get -9 for the answer.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    Quote Originally Posted by matthayzon89 View Post
    WRONG! Carry into MSB is 1 and carry out of MSB is 0. Overflow condition occurs!
    To understand that explanation you look into the overflow flag.

    This flag is set whenever the most significant digit is changed. In the 8bit unsigned mathematical operation you perfomed, adding 01100100 to itself will set the overflow flag because the result is 11001000.

    Now, with the overflow flag set, you know that the sign didn't change. Meaning, you must interpret 11001000 as a positive number, because the numbers being added had their most significant digit as 0 and were thus positive (the overflow flag is never set when you add or subtract positive and negative numbers. Only when both are either positive or negative).

    Quote Originally Posted by matthayzon89 View Post
    Why does: (- 40 * 144) mod 71 =62
    It's probably the result of one of the methods applied to the mod operation when there are negative operands involved. See here.
    Last edited by Mario F.; 09-19-2010 at 09:12 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Quote Originally Posted by matthayzon89 View Post
    Also,
    Why does: (- 40 * 144) mod 71 =62???!

    When I calculate it, I get -9 for the answer.
    It's just the way the C language chose to implement it.

    From Modulo operation - Wikipedia, the free encyclopedia

    When either a or n are negative, this naive definition breaks down and many programming languages differ in how these values are defined.

    ...This means there are two possible choices for the remainder, one negative and the other positive, and there are also two possible choices for the quotient. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of a and n.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    I figured it out, its actually far more simple than what I thought....

    -40 * 144 mod 71 =
    [-40 mod 71] * [144 mod 71] mod 71= 31 * 2 mod 71= 62 mod 71 = 62

    thanks for reply's.


    NOW, to the next problem...
    Can someone help me understand how this works....
    -55 mod 7= 1 ?!?!?! what the hell



    thanks in advance...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by matthayzon89
    Can someone help me understand how this works....
    -55 mod 7 = 1 ?!?!?!
    Well, -55 is indeed congruent to 1 (modulo 7). Consider: 8 is congruent to 1 (modulo 7). If you subtract 1 from 8, you get 7, which is perfectly divisible by 7. Likewise, if you subtract 1 from -55, you get -56, which is perfectly divisible by 7. That said, since you are probably really testing for a remainder operation here, don't be surprised if the results are different, as was pointed out to you earlier in this thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The way mod works in practice is the same whether the numerator is positive or negative.
    It's the intermediate division that's confusing because it rounds towards zero when division is done in isolation, yet appears to round towards left within the mod operation.

    Finding how many times the denominator divides:
    -55 / 7 = -8 (rounding in the left direction on the number line)
    Finding remainder:
    -55 - (7 * -8) = 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 06:24 AM
  2. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  3. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21