0

I wanted to know what does ~ operation on int value does. I coded and printed ~a with a assigned as 3 and it gave -4. Please tell me its usage.

8 Answers8

6

~ is bitwise NOT operator (complement). It toggles bit 0 to 1 and 1 to 0. On a 32 bit machine decimal 3 is

00000000 00000000 00000000 00000011  (2's complement)  

The bitwise complement is equal to the two's complement of the value minus one (~x = −x − 1). Therefore ~3 is

11111111 11111111 11111111 11111100 = -4
haccks
  • 104,019
  • 25
  • 176
  • 264
3

Unary operator that produces the bitwise NOT or complement of a binary value.

In your case using a signed integer:

 a = 3 = 0b00000011

~a = -4 = 0b11111100
PJRobot
  • 1,306
  • 13
  • 14
1

It's the unary complement operator, or bitwise not operator. It flips all the bits in a number.

In the case of signed integers, usually the highest bit stores the sign (two's complement storage), so complementing a positive number yields a negative one, and vice-versa (except for 0 and the minimum negative value).

Quentin
  • 62,093
  • 7
  • 131
  • 191
1

It is the bitwise complement operator. More information can be seen here, but essentially as has been stated by the other answers it flips the binary bits of, in your example, an integer from either 0 to 1 or 1 to 0.

An example would be if you had the decimal value 15 represented in binary format as

1111

and if you were to take the bitwise complement of 15 like ~15 the binary format of that would produce

0000

which is obviously 0 in decimal.

alacy
  • 4,972
  • 8
  • 30
  • 47
  • The `~15 == 0` example only works on 4-bit unsigned integers, which don't normally appear in C (other than structure bitfields). – pat Dec 31 '14 at 16:33
  • @pat you're absolutely correct. I was only using it as an example of how the bits change via the complement. – alacy Dec 31 '14 at 16:51
1

One's complement. ~ flips the bits. In your example, 00000011 is changed to 11111100, assuming it's an 8 bit number.

Refer to the following chart of Bitwise operations in C.

enter image description here

Jobs
  • 3,317
  • 6
  • 26
  • 52
1

~ is bitwise NOT operator, this flips the bit pattern. Assume the bit value is 1010, this bitwise NOT operator will flip the pattern to 0101

0

Inverts all the bits, it turns 00010101 -> 11101010 for example

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

The ~ operator takes an operand of any integer type, and returns the inverted-bit value.

It does not change the value of the operand itself (in opposed to ++ and --, for example).

It is useful mostly for bit masking.

Example #1:

  • Compute a mask with bit #5 set to 0 and all other bits set to 1:

    int mask = ~(1<<5);

  • Given an integer variable x, set all bits to 1 and keep bit #5 at its current value:

    x |= mask;

Example #2:

  • Compute a mask with bit #5 set to 1 and all other bits set to 0:

    int mask = (1<<5);

  • Given an integer variable x, set all bits to 0 and keep bit #5 at its current value:

    x &= ~mask;

It is also quite useful on beginner job interviews...

barak manos
  • 29,648
  • 10
  • 62
  • 114