Euphemia
Euphemia

Reputation: 65

How to use bitwise NOT operator with shift operator in C#?

I don't understand how this expression works.

~(1 << 1) = -3

What I do understand is that 1 << 1 has a value of 10 in binary and 2 in base 10. How did it get a -3 with the NOT operator? How does shift operators work with NOT operators?

Upvotes: 2

Views: 269

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502036

The bitwise inverse operator is entirely separate from the shift here.

You've started with input of 10 (binary) - which has a full 32-bit representation of

00000000_00000000_00000000_00000010

The bitwise inverse is therefore:

11111111_11111111_11111111_11111101

... which is the binary representation of -3 (in 32-bit two's complement).

Upvotes: 11

Related Questions