4 ms·
Rephrasing: Let's say x=000110 for example Invert all bits of x, call that number inv(x)=111001 If we add them together, "the gears mesh together" and we get
by cousin_it 3y ago
Rephrasing:
Let's say x=000110 for example
Invert all bits of x, call that number inv(x)=111001
If we add them together, "the gears mesh together" and we get x+inv(x)=111111
Adding 1 to that, let the carry just drop off the left side, we get x+inv(x)+1=000000
So it's natural to take inv(x)+1 as a representation of -x.
- arun-mani-j 3y agoThank you, I wish I saw this an year ago when I was learning 2's complement for digital systems subject. Many articles I read said that 2's complement helps in arithmetic, but I was not able to understand "how". The naive me was like - why do they complement and set a sign bit? We can just use a sign bit right? So if 0010 means +2, then why not represent -2 as 1010. What's the need for the complement part at all?! Then only I realized that we are able to retain x + (-x) = 0 using this 2's complement representation. :)
- thaumasiotes 3y agoHere's a very brief description of two's complement that also happens to be a complete description: Two's complement is ordinary integer arithmetic. There is no difference between two's complement arithmetic and unsigned arithmetic. All calculations are literally identical. Unsigned integer arithmetic is modular arithmetic with equivalence classes having labels between 0 (inclusive) and 2^n (exclusive), while two's complement arithmetic is the same thing, but the equivalence classes have labels between negative 2^{n-1} (inclusive) and 2^{n-1} (exclusive). Here's a table of three-bit values with "unsigned" and "two's complement" labels: 100 4 -4 101 5 -3 110 6 -2 111 7 -1 000 0 0 001 1 1 010 2 2 011 3 3 100 4 -4 101 5 -3 110 6 -2 111 7 -1 Note that when you're working in mod 8, 5 and -3 are the same value, so this is nothing but a labeling difference.
- laszlokorte 3y agoYou might find it useful to play around with this: https://thesis.laszlokorte.de/demo/number-circle.html https://thesis.laszlokorte.de/demo/number-circle.html And notice how „just using the sign bit“ and the 2compl are just different ways to align interepretations with bit patterns. But using the 2compl is the alignment that does not require special cases for handling the negative numbers as opposed to the positive numbers.
- secondcoming 3y agoA sign bit also means you have deal with positive and negative zero.