Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1's complement using ~ in C/C++

Tags:

People also ask

How do you do one's complement in C?

The one's complement operator ( ~ ), sometimes called the bitwise complement operator, yields a bitwise one's complement of its operand. That is, every bit that is 1 in the operand is 0 in the result. Conversely, every bit that is 0 in the operand is 1 in the result.

What is 1's complement with example?

To get 1's complement of a binary number, simply invert the given number. For example, 1's complement of binary number 110010 is 001101. To get 2's complement of binary number is 1's complement of given number plus 1 to the least significant bit (LSB).

What is one's complement in C++?

To make this, we will shift 1 to the left c number of times, then subtract 1 from it. After shifting 1 to the left 5 times, it will be 100000, then subtract 1, so it will be 11111. After that perform XOR operation with the 11111 and 10110 to get the complement.

How do you represent in 1's complement?

For n-bit word, positive number in 1's complement form is represented by a zero followed by the binary magnitude as in sign-magnitude and 2's complement. Representation of +ve and -ve zero. 1's complement can also be formed by compelementing all the bits in N.


I am using Visual Studio 2013.
Recently I tried the ~ operator for 1's complement:

int a = 10;
cout << ~a << endl;

Output is -11

But for

unsigned int a = 10;
cout << ~a << endl;

the output is 4294967296

I don't get why the output is -11 in the case of signed int. Please help me with this confusion.