What is “2's Complement”?

前端 未结 23 2483
名媛妹妹
名媛妹妹 2020-11-21 05:59

I\'m in a computer systems course and have been struggling, in part, with Two\'s Complement. I want to understand it but everything I\'ve read hasn\'t brought the p

23条回答
  •  无人共我
    2020-11-21 06:35

    2's complement is very useful for finding the value of a binary, however I thought of a much more concise way of solving such a problem(never seen anyone else publish it):

    take a binary, for example: 1101 which is [assuming that space "1" is the sign] equal to -3.

    using 2's complement we would do this...flip 1101 to 0010...add 0001 + 0010 ===> gives us 0011. 0011 in positive binary = 3. therefore 1101 = -3!

    What I realized:

    instead of all the flipping and adding, you can just do the basic method for solving for a positive binary(lets say 0101) is (23 * 0) + (22 * 1) + (21 * 0) + (20 * 1) = 5.

    Do exactly the same concept with a negative!(with a small twist)

    take 1101, for example:

    for the first number instead of 23 * 1 = 8 , do -(23 * 1) = -8.

    then continue as usual, doing -8 + (22 * 1) + (21 * 0) + (20 * 1) = -3

提交回复
热议问题