Question #27865

a=a-~b-1; how this works please explain

Expert's answer

"~" in C++ is bitwise operation NOT. Thebitwise NOT, or complement, is an unary operation that performs logical

negation on each bit, forming the ones' complement of the given binary value.

Bits that are

0 become 1, and those that are 1 become 0. The bitwisecomplement is equal to the two's complement of the value minus one. If two's

complement arithmetic is used, then ~x = - x - 1.

In your case

a = a - ~b - 1 = a - (-b + 1) - 1 = a + b;

negation on each bit, forming the ones' complement of the given binary value.

Bits that are

0 become 1, and those that are 1 become 0. The bitwisecomplement is equal to the two's complement of the value minus one. If two's

complement arithmetic is used, then ~x = - x - 1.

In your case

a = a - ~b - 1 = a - (-b + 1) - 1 = a + b;

## Comments

## Leave a comment