why -3==~2 in C#

Because two’s complement bit-arithmetic makes it so

Cribbed from the wikipedia page and expanded:

Most
Significant
Bit          6  5  4  3  2  1  0   Value
0            0  0  0  0  0  1  1   3
0            0  0  0  0  0  1  0   2
0            0  0  0  0  0  0  1   1 
0            0  0  0  0  0  0  0   0
1            1  1  1  1  1  1  1   -1
1            1  1  1  1  1  1  0   -2
1            1  1  1  1  1  0  1   -3
1            1  1  1  1  1  0  0   -4

So you get:

0  0  0  0  0  0  1  0  =  2
1  1  1  1  1  1  0  1  = -3

And as you can see, all the bits are flipped, which is what the bitwise NOT operator (~) does.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)