开发者

Explanation of Bitwise NOT Operator

开发者 https://www.devze.com 2023-01-27 11:36 出处:网络
Why is it that 开发者_开发百科the bitwise NOT operator (~ in most languages) converts the following values like so:

Why is it that 开发者_开发百科the bitwise NOT operator (~ in most languages) converts the following values like so:

-2 -> 1

-1 -> 0

0 -> -1

1 -> -2

Shouldn't -2 convert to 2, 1 convert to -1, etc.?


See two's complement for the representation of negative integers in many languages. As you can see, -2 is represented by 1111110; if you invert all those bits you get 0000001, i.e. a value of 1.


It helps if you look at it in binary.

First of all, as you know, negative numbers are expressed as (highest possible unsigned number plus 1 minus value). So -1 in a 16-bit integer, which has the highest unsigned value of 65535, would be 65536-1=65535, i.e. 0xffff in hex, or 1111 1111 1111 1111 in binary.

So:

1 in binary = 0000 0000 0000 0001

NOT on all bits would result in 1111 1111 1111 1110. That, in decimal, is 65534. And 65536 minus 65534 is 2, so this is -2.


Most (all?) modern architectures use two's complement to represent signed integers. The bitwise NOT is thus the complement of the integer minus one.


Dim mask As Integer = -1
'11111111111111111111111111111111

For x As Integer = -3 To 3
    Dim i As Integer = x
    Debug.WriteLine("")
    Debug.WriteLine("'" & Convert.ToString(i, 2).PadLeft(32, "0"c) & " > Num = " & i.ToString)

    i = i Xor mask 'reverse the bits (same as Not)
    Debug.WriteLine("'" & Convert.ToString(i, 2).PadLeft(32, "0"c) & " > Not = " & i.ToString)

    i += 1 'convert to two's complement
    Debug.WriteLine("'" & Convert.ToString(i, 2).PadLeft(32, "0"c) & " > 2's Comp = " & i.ToString)
Next

'debug results

'11111111111111111111111111111101 > Num = -3
'00000000000000000000000000000010 > Not = 2
'00000000000000000000000000000011 > 2's Comp = 3

'11111111111111111111111111111110 > Num = -2
'00000000000000000000000000000001 > Not = 1
'00000000000000000000000000000010 > 2's Comp = 2

'11111111111111111111111111111111 > Num = -1
'00000000000000000000000000000000 > Not = 0
'00000000000000000000000000000001 > 2's Comp = 1

'00000000000000000000000000000000 > Num = 0
'11111111111111111111111111111111 > Not = -1
'00000000000000000000000000000000 > 2's Comp = 0

'00000000000000000000000000000001 > Num = 1
'11111111111111111111111111111110 > Not = -2
'11111111111111111111111111111111 > 2's Comp = -1

'00000000000000000000000000000010 > Num = 2
'11111111111111111111111111111101 > Not = -3
'11111111111111111111111111111110 > 2's Comp = -2

'00000000000000000000000000000011 > Num = 3
'11111111111111111111111111111100 > Not = -4
'11111111111111111111111111111101 > 2's Comp = -3


This is due to how negative numbers are represented as bits. For this most commonly Two's Complements are used.

-2 happens to be 1111110 in this notation, which negated is 1


This is because the bit-wise operator literally inverts each bit in the word.
It is NOT strictly an arithmetic operation, it is a logic operation.

-2 == %1110, ~-2 == ~%1110 = %0001 == 1
-1 == %1111, ~-1 == ~%1111 = %0000 == 0

and so on.

To go from -2 to 2, and 1 to -1 you need to use the arithmetic negation operation.


The numbers in computer systems are stored as 2 complementary. If number is positive then 2 complement of positive number is same.But for the negative number it is different.

  1. -2 -> 1

    Here -2 will be stored in computer as 1110 (i.e. 2's complement of -2). Now ~ of 1110 is 0001. As 0001 is positive number it will be stored in computer as 0001 (i.e. 1)

  2. -1 -> 0

    Here -1 will be stored in computer as 1111 (i.e. 2's complement of -1). Now ~ of 1111 is 0000.As 0000 is positive number it will be stored in computer as 0000 (i.e. 0)

  3. 0 -> -1

    Here 0 will be stored in computer as 0000 (i.e. 2's complement of 0). Now ~ of 0000 is 1111. As 1111 is negative number it will be stored in computer as 0001 (i.e. -1) (since MSB is set for 1111 the number will be negative)

  4. 1 -> -2

    Here 1 will be stored in computer as 0001 (i.e. 2's complement of 1). Now ~ of 0001 is 1110. As 1110 is negative number it will be stored in computer as 0010 (i.e.-2) (since MSB is set for 1110 the number will be negative)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号