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?

Jon Skeet
people
quotationmark

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).

people

See more on this question at Stackoverflow