0
$\begingroup$

I saw online that if you want to convert a binary number to a negative binary number, you add 1.However, I don't understand why you do that.In a forum I saw someone explaining the following:

A little rule that I use when I need to represent negative numbers in binary is
~i = -i-1.
That is, the bitwise inversion of "i" is equivalent to negative "i" less one.
In your example, you're looking for the binary representation of -192. Since -192 = -191-1, the following statement is true:
~191 = -192.

Unfortunately, I don't understand this explanation.I went on further to understand the bit wise inversion of binary and I did understand that: it's only changing 1 into 0 and o into 1. Then I saw the following explanation from Microsoft:

~5 == 11111111 11111111 11111111 11111010 == -6 in decimal.

How do you really get -6 from ~5?How that gives you negative -6. Isn't 6 is 110?And if you add 1 to make it negative, shouldn't it be 111?Can someone please explain?

$\endgroup$
  • $\begingroup$ This is no mathematics question. This is about the two's / one's complement representation of a binary number. Btw, you must also take the bitlength of the variables into account. Here, we assume that we're dealing with int32 (32-bit wide) numbers, and so if $5 = (101)_2$, then $~5 = ( \underbrace{1...1}_{32-3\text{ ones}}010)_2$. $\endgroup$ – Maximilian Gerhardt Jun 16 '16 at 21:21
  • $\begingroup$ The $\LaTeX$ swallowed the ~ there, sorry. The bitwise inverse of $5$ is then equal to that on the right. And it's $32-3 = 29$ ones because the number is 32 bits wide, meaning $5 = (00000000 \;00000000\;00000000\;00000101)_2$ (this saved in memory in 4 bytes). Not only the 3 bits $101$ are inverted on the right, but also the $29$ bits (all $0$) are inverted. You should start reading into (en.wikipedia.org/wiki/Two%27s_complement) or (cs.uwm.edu/~cs151/Bacon/Lecture/HTML/ch03s09.html) $\endgroup$ – Maximilian Gerhardt Jun 16 '16 at 21:35
  • $\begingroup$ I now understand it's because of 32 bits.What I don't understand is why you subtract 1.Why the bitwise inversion of "i" is equivalent to negative "i" less one. $\endgroup$ – Rafi Jun 16 '16 at 21:38
  • $\begingroup$ That's because of the the two's compliment. If the left-most bit is $1$, this says that the saved number is negative. Taking the two's compliment negates the sign of a number $a$ and is equivlent to computing $~a + 1$. Therefore $-a = ~a + 1$. Then $-a - 1 = ~a$. Why the two's compliment works that way is what you should in the links above or additional googling (youtube.com/watch?v=lC5ckH5ODL4). $\endgroup$ – Maximilian Gerhardt Jun 16 '16 at 21:49
2
$\begingroup$

To find the negative of a number $a$ you need to find a number $b$ that, when added to $a$, gives zero.

In binary this is done using the 2's complement method. The 2's complement method goes as follows: invert all bits and add $1$.

Why does it work?

Let's take a number $0101$. Inverting it gives $1010$. Adding These two numbers gives us $1111$. Actually, for any $n$ the following is true:

$n+\overline{n}= 1...1111$

Now adding a $1$ will make the $1...1111$ overflow and it will turn into $0...0000$.

Notice the overflow: In "real" mathematics the answer would be $1000...000$, but in computer arithmetic there is only a finite amount of bits to represent numbers. So therefore the answer is $0...000$ with a carry out of $1$.

So:

$n+\overline{n}+1=0 \iff \overline{n}+1=-n$

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Good, but I would specify you're specifically talking about computer arithmetic, i.e. arithmetic mod $2^n$. $\endgroup$ – 6005 Aug 6 '16 at 21:07
  • $\begingroup$ Yep, computer arithmetic. In "real" maths there isn't an overflow. $\endgroup$ – gilianzz Aug 7 '16 at 11:49
2
$\begingroup$

"How do you really get -6 from ~5?How that gives you negative -6. Isn't 6 is 110?And if you add 1 to make it negative, shouldn't it be 111?Can someone please explain?"

You invert 5 and add a one. You don't get -6 by simply adding a one to 6. Gilianzz' answer explains it the best.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.