When to use the bitwise and operator (&)? [duplicate] - python

This question already has answers here:
Real world use cases of bitwise operators [closed]
(41 answers)
Bitwise operation and usage
(17 answers)
Closed 12 months ago.
I understand that the bitwise and operator (&) is equivalent to a product of two bit values. When would I use it?
Please also help me understand what num&1 does in the code below:
def func(num):
n = 1 + func((3*num+1) if num&1 else (num>>1))
return n

As the comments mentioned, num&1 is a bitwise AND between num and 1.
Since 1 in binary is ...000000001, the AND will result True iff the least significant bit of num is 1, in other words, if it is odd (here some explanation of binary)

Related

Curious about the function of ~in the following code [duplicate]

This question already has answers here:
The tilde operator in Python
(9 answers)
Closed 1 year ago.
pd_selftest = pd_selftest[pd_selftest['SICCD'] != 0]
pd_selftest = pd_selftest[~pd_selftest['SICCD'].isnull()]
I'd like to know what the function of the ~ is in the above code.
That's the bit-wise invert or not operator. So, it returns only those lines where the SICCID column is not null. I would probably use the word not in this case.

This is regarding Python Exponentiation [duplicate]

This question already has answers here:
Why is exponentiation applied right to left?
(4 answers)
Closed 2 years ago.
print (2**3**2)
Answer is 512.
Why 512 is answer not 64? Because ((2^3)^2) = 64
I want to know the inside math operation of print (2** 3**2)
The order of operations for exponentiation is right-to-left, not-left-to right. So:
2**3**2
is interpretted as:
2**(3**2) = 2**(9) = 512

How come 2 ^ 3 = 1 in python 3.9 [duplicate]

This question already has answers here:
What does the ^ (XOR) operator do? [duplicate]
(6 answers)
Closed 2 years ago.
Shouldn't it be 8?
The same thing goes with 3 ^ 2. I also got 1.
This is confusing...
In Python, ^ is a bitwise XOR operator. I believe what you're looking for is the exponent operator, **. An example would be 2**3 which outputs 8, like I believe you were looking for.
The ^ operator does a bitwise XOR operation. In python to do power calculation use pow() function:
pow(3,2)
Or use **
3**2

Why "~False" is -1 in Python? [duplicate]

This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
Why "~False" is -1 in Python?
I was using a boolean variable in Python. When I try to do not of that, it returns -1. I want to understand why so? why a variable a changing its data-type duo to this operation.
Trying to add more details
b0 = False
print(type(b0))
b0 = ~b0
print(type(b0))
>>bool
>>int
The tilde ~ is the bitwise 'not' operator, not the boolean 'not' operator. To do a not you probably want 'not False'.
The reason for it changing its data type is that it treats False as binary 0 and then flips it to -1.

How short circuit code works in python [duplicate]

This question already has answers here:
and / or operators return value [duplicate]
(4 answers)
Closed 6 years ago.
This looks like a short circuit way of writing code but I just can't understand it. Is there a specific way to read this kind of short circuit.
e.g:
n = n and int(n)
n = n or int(n)
and conditions return the last truthy value or the first falsy one. So if n is falsy, n will remain whatever value it was. If n is truthy, it will be cast to an int.

Categories

Resources