What is “2's Complement”?

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

    Imagine that you have a finite number of bits/trits/digits/whatever. You define 0 as all digits being 0, and count upwards naturally:

    00
    01
    02
    ..
    

    Eventually you will overflow.

    98
    99
    00
    

    We have two digits and can represent all numbers from 0 to 100. All those numbers are positive! Suppose we want to represent negative numbers too?

    What we really have is a cycle. The number before 2 is 1. The number before 1 is 0. The number before 0 is... 99.

    So, for simplicity, let's say that any number over 50 is negative. "0" through "49" represent 0 through 49. "99" is -1, "98" is -2, ... "50" is -50.

    This representation is ten's complement. Computers typically use two's complement, which is the same except using bits instead of digits.

    The nice thing about ten's complement is that addition just works. You do not need to do anything special to add positive and negative numbers!

提交回复
热议问题