This question already has answers here:
'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays?
(8 answers)
Closed 6 years ago.
sorry about this basic question, I just a beginner in Python programming.
From my undertsanding, & and "and" are the same things, and "&" is just a shorhand for "and" so if I use Python's dataframe
df[ (df.StateAb == "NSW") & (df.PartyAb == "LP") ]
this compliles OK,but if I type
df[ (df.StateAb == "NSW") and (df.PartyAb == "LP") ]
then it cannot be compiled correctly.
so what's the difference between "and" and "&",
I found this one useful:
1 and 2
>> 2
1 & 2
>> 0
The first result is due to short circuiting. Python tests 1 and finds it true and returns the 2. But, the second part does 01 (Binary 1) & 10 (Binary 2) hence evaluating to 00 (1 & 0, 0 &1) , which is 0.
Related
This question already has answers here:
What does & mean in python [duplicate]
(5 answers)
Bitwise operation and usage
(17 answers)
Python if statement without == operator
(3 answers)
'if' not followed by conditional statement
(7 answers)
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 3 months ago.
I have the following code segment in python
if mask & selectors.EVENT_READ:
recv_data = sock.recv(1024)
if recv_data:
data.outb += recv_data
else:
print(f"Closing connection to {data.addr}")
Would I read this as: 'if mask and selectos.EVENT_READ are equivalent:'
And similarly: 'if recv_data is equivalent to true:'
Help is greatly appreciated!
For the second assumptions, yes.
if var_name: is shorthand of saying if var_name evaluates to a truthy value.
Your first assumption is wrong though, a logical AND operation in python is actually and, not & - many languages do use an ampersand as a logical and, but this is usually a double ampersand, as in &&. A single ampersand is usually a bitwise AND, not a logical AND.
So in your code above, the first if statement is doing a bitwise (binary) AND on the selectors.READ_EVENT with a bitmask of mask. Basically its a way of asking if the values match, in a binary way. So if READ_EVENT is 010 and the mask is also 010, then the logic evaluates to true. Otherwise
All values have an inherent Boolean value. For the bytes value returned by sock.recv, the empty value b'' is false and all other values are true. This is a short, idiomatic way of writing
if recv_data != b'':
...
The & is a bitwise AND operator. The result of mask & selectors.EVENT_READ produces a value where a bit is 1 if both mask and selectors.EVENT_READ have a 1, and 0 otherwise. The result of that is an integer which may or may not be 0 (and for int values, 0 is false and all others are true).
Basically, mask & selectors.EVENT_READ is true if and only if any of the bits set in selectors.EVENT_READ are also set in mask.
This question already has answers here:
Boolean operators vs Bitwise operators
(9 answers)
Closed 4 months ago.
I'm trying to get into the book "Hacker's Delight." The first formula in chapter 2 is x & (x -1) and is supposed to "turn off the rightmost 1-bit in a word producing 0 if none
(e.g. 01011000 -> 0101000)." I have no idea why someone would want to do this.
I translated this into python as
bin(0b1011000 and (0b1011000 - 1)) and got
'0b1010111'. Is this correct?
I tried leaving out the "b" designating leading zeros and got this wild result '0b11110110110100110111'.
Am I close to correct?
Try these a few examples and see if you can tell the correctness yourself:
>>>x = 15
>>>bin(x)
'0b1111'
>>>x & (x -1)
14
>>>bin(14)
'0b1110'
# now try this x = 10
>>>x = 10
>>>bin(x)
'0b1010'
>>>x & (x - 1)
8
>>>bin(8)
'0b1000` # <---- see the rightmost bit is gone *rightmost 1*
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)
This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 1 year ago.
Can someone explain the difference between these two expressions in Python:
(-1)**2 == 1
-1**2 == -1
Why do the parentheses change the outcome?
The parentheses means the whole value inside is will be raised to the power 2.
(-1)**2 == 1
So -1*-1 is 1
No parentheses means the - will be taken out of the equation and added to the end of the answer.
1) -1**2
2) 1**2
3) 1
4) -1
Python handles this the same way the world does :)
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.