# Complement to 1 and complement to 2

11

Why in this code:

``````int a = 2, b = 0, c = 0;
c = -a; // resultado c = -2
c = ~b; // resultado c = -1
``````

Why in the first case `c=-2` and in the second `c=-1` ?

asked by Rubén 15.07.2016 в 12:11
source

16

1. Operator `-`

This operator is simple, you simply get the same value changed sign. If it is positive, it becomes negative; and if it is negative, it becomes positive.

So in your case `-a` where `a=2` the result is `-a=-2`

2. Operator `~` called bitwise complement

This is more interesting. What this operator does is exchange the representation in binary of the number. That is, let's say the `~2` :

• 2 in binary is: `0000 0010`
• we do an exchange of its bits (all `0` to `1` and all `1` to `0` ) the result is: `1111 1101` which is the representation in binary of `-3` .
• So `~2 = -3`

Let's go to your specific case, which is `~0`

• we start from the `0` that in binary is: `0000 0000`
• We exchange all the bits: `1111 1111` which is the representation in binary of `-1` .
• So `~0 = -1`

You have more documentation from the operators here (for java).

To finish simply commenting how they say in this answer from SO original:

## The bitwise operator (~) ONLY EXCHANGES BITS. It is up to the machine to interpret them.

Why do I comment on this? To the naked eye the operator `~` is simply to change sign and subtract `1` . That is:

``````~x = -x - 1
``````

I imagine that there was your question. But it depends on the computer the interpretation of the operator bits `~` is not always the same.

But as it says Paul Vargas in the chat :

Since the question is labeled with there should be no results rare (that there are, for example, in when work with `unsigned` - see this link )

5

In the first assignment `c = -a;` you are using the "minus" symbol to deny the number contained in `a` so the value of `c` will be the negative of `a` : -2. It is the equivalent of multiplying by -1.

In the second case you are using the bit-level denial operator `~` also called "tilde". This operator inverts the 0s and 1s of the number. In this case the number is 0 and its representation in binary is 000000000. When applying the binary negation the resulting binary number is 11111111.

The binary number 11111111 represents the number -1 in decimal given that the representation "complement to 2" is used. To learn more about the add-on to 2 you can read the wikipedia entry

5

First of all we can start by reviewing what each of the operations is about:

• ### Supplement to 1

The complement to 1 of a binary number is obtained by changing `0``1` and `1``0` . That is, each bit is changed by its complement. For example (using a `byte` ):

``````  0 0 0 0 0 0 1 0    Original        ( =  2 )
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
1 1 1 1 1 1 0 1    Complemento a 1 ( = -3 )
``````

See Wikipedia on Complemento a uno .

• ### Complement to 2

The complement to 2 of a binary number is obtained by taking the complement to 1 of the number and then adding 1 to the least significant bit ( LSB or Least Significant Bit ). Taking the previous example:

``````  0 0 0 0 0 0 1 0    Original        ( =  2 )
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
1 1 1 1 1 1 0 1    Complemento a 1 ( = -3 )
+               1
───────────────
1 1 1 1 1 1 1 0    Complemento a 2 ( = -2 )
``````

The complement to 2 of 0 is 0 ( `00000000``11111111``00000000` ) and the complement 2 of the most negative number, `Byte.MIN_VALUE` , is the same ( `10000000``01111111``10000000` ).

See Wikipedia on Complemento a dos .

Now, analyzing the code:

• ### `c = -a; // resultado c = -2`

If `a = 2` (positive), then `c = -a` is calculated:

``````0000 0000 0000 0000 0000 0000 0000 0010    Original         ( =  2 )
1111 1111 1111 1111 1111 1111 1111 1101    Complemento a 1  ( = -3 )
1111 1111 1111 1111 1111 1111 1111 1110    Sumando 1 al LSB ( = -2 )
``````

Or the complement to 2.

See §15.15.4 within the The Java Language Specification . < br>

• ### `c = ~b; // resultado c = -1`

If `b = 0` (positive), then `c = ~b` is calculated:

``````0000 0000 0000 0000 0000 0000 0000 0000    Original         ( =  0 )
1111 1111 1111 1111 1111 1111 1111 1111    Complemento a 1  ( = -1 )
``````

See §15.15.5 within the The Java Language Specification .