Python – Boolean / Bitwise operation

By | January 4, 2017

The logical operator and the bitwise operator are similar to each other, so the difference makes me confused.

What is the difference between the logical operator “and” and the bitwise operator “&”?
What is the difference between the logical operator “or” and the bitwise operator “|”?
What is the difference between the logical operator “not” and the bitwise operator “~”?

Several experiments have confirmed the differences.

In conclusion, the logical operator is an operator for TRUE and FALSE(boolean). Therefore, it is possible to derive an erroneous value when used as bit operator using a number. You must use bitwise operators when performing bit operations.
Use of logical operators for numbers other than TRUE and FALSE does not cause an explicit error message, so you should be more careful to use it because unintended results can be calculated.

[Difference comparison]
1. Difference between ‘and’ and ‘&’
2. Difference between ‘or’ and ‘|’
3. Difference between ‘not’ and ‘~’

[Other bit operators]
4. shift operator (<<, >>)
5. xor operator (^)

 

Decimal 240 (0b11110000) is used as the base and 0b10101010 and 0b1010 are used as the mask.

 

1. The difference between ‘and’ and ‘&’

[& Basic operation]
1 & 1 = 1
0 & 1 = 1
1 & 0 = 1
0 & 0 = 0

0b11110000 (value)
0b10101010 (mask0)
————– (&)
0b10100000 (result)

The expected value when 0b11110000 and 0b10101010 are operated with ‘&’ is 0b10100000.
However, when using “and”, the wrong value of 0b10101010 appears, and when using “&”, the normal value is obtained as 0b10100000.
0b11110000 (value)
0b00001010 (mask1)
————– (&)
0b00001010 (result)

This time, I wondered what the number of digits would be when I did masking. If the number of digits does not match, put 0 in front of the digits to calculate the number of digits. As with the previous results, the “&” operation showed a normal result, but the “and” output 0.
2. The difference between ‘or’ and ‘|’ 

[| Basic operation]
1 | 1 = 1
0 | 1 = 0
1 | 0 = 0
0 | 0 = 0

0b11110000 (value)
0b10101010 (mask0)
————– (|)
0b11110000 (result)

‘|’ , The results are somewhat different from those of the previous ‘&’. ‘or’ and ‘|’,  The same result can be seen among these.
0b11110000 (value)
0b00001010 (mask1)
————– (|)
0b11111010 (result)

The same result was obtained for the second example as well.
3. The difference between ‘not’ and ‘~’

In the case of ‘not’, it showed some understandable results. Often, a nonzero value is considered as TRUE, so a nonzero value with “not” indicate a result of zero.

In the case of ‘~’, I actually got some unfamiliar results.

In fact, the first expected value was conversion values like 0 to 1, 1 to 0.

~ (101010) -> 010101

However, if you attach ‘~’, it seems to express 2’s complement.
The python document describes the behavior of ‘~’ as follows:

The bitwise inversion of x is defined as – (x + 1).

– (0b11110000) = – (0b11110000 + 1) = – 0b11110001

The intended values are shown.

If you need to convert 1’s, that is 11110000 -> 00001111, you need to convert by xor instead of ‘~’.

0b11110000 (value)
0b11111111 (mask)
————– (xor)
0b00001111 (result)

 

4. shift operator (<<, >>)

In the case of shift operation, it moves the bit.
0b11110000 << 2, it moves 2 spaces to the left.

That is, since it is 0b0011110000, if << 2 is set, it becomes 0b1111000000.

 

“>> 2” is also similar. The difference is that 0 is not omitted to the right of the binary representation.

That is, since it is 0b11110000, if >> 2, 0b111100 is obtained.

 

5. xor operator (~)

[^ Basic operation]
1 ^ 1 = 0
0 ^ 1 = 1
1 ^ 0 = 1
0 ^ 0 = 0

Unlike ‘and, &’, ‘or, |’, ‘not, ~’, in the case of xor, there is no xor in logical operation. So there is no confusing or mistaken part.

0b11110000 (value)
0b10101010 (mask)
————– (xor)
0b01011010 (result)
0b11110000 (value)
0b00001010 (mask)
————– (xor)
0b11111010 (result)

It can be confirmed that the above calculation is normally performed.

 

 

3 thoughts on “Python – Boolean / Bitwise operation

  1. Tisha

    I have read several good stuff here. Definitely worth borkoamking for revisiting. I surprise how much effort you put to make such a magnificent informative web site.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *