What is “2's Complement”?

前端 未结 23 2530
名媛妹妹
名媛妹妹 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:34

    In simple term 2's Complement is a way to store negative number in Computer Memory. Whereas Positive Numbers are stored as Normal Binary Number.

    Let's consider this example,

    Computer uses Binary Number System to represent any number.

    x = 5;
    

    This is represented as 0101.

    x = -5;
    

    When the computer encouters - sign, it computes it's 2's complement and stores it. i.e 5 = 0101 and it's 2's complement is 1011.

    Important rules computer uses to process numbers are,

    1. If the first bit is 1 then it must be negative number.
    2. If all the bits except first bit are 0 then it is a positive number because there is no -0 in number system.(1000 is not -0 instead it is positive 8)
    3. If all the bits are 0 then it is 0.
    4. Else it is a positive number.

提交回复
热议问题