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*
Related
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:
Specify float_format differently for each column (scientific notation vs decimal precision)
(2 answers)
Closed 2 years ago.
The output of Jupyter notebook was not able to show the value. May I know how I can change the width to fix the display problem?
one thing you must try is, reduce the number of digits after the period(.)
in pandas, there is an option
initial output:
random
0 2.339650e-03
1 6.714034e-02
2 1.381005e-15
3 4.846619e-05
4 6.375477e-06
Changed the pattern by using the following code:
dd = df.apply(lambda x: '%.10f' % x, axis=1)
corrected output:
0 0.0023396498
1 0.0671403367
2 0.0000000000
3 0.0000484662
4 0.0000063755
instead of .10f you and use any less number. because there is no need to put that much digit after the period
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 3 years ago.
Pretty new to python, facing a problem that requires basically the opposite of the remainder "%" function. For example, if I wanted to divide 81.5 by 20, my output would be 4. My best attempt is as follows:
amount = 81.504
round(amount, 2)
num20s = amount / 20
int(num20s)
I've tried several different combinations of the above code, but nothing has worked so far. This is the closest I've gotten to what I want, but it won't work in edge cases, and for some reason still represents the number with a ".0" at the end, so that last line must not be doing anything.
Integer division operator in python is "//".
>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4
This question already has answers here:
What does & mean in python [duplicate]
(5 answers)
Closed 3 years ago.
I mistakenly wrote the line of code, I expected it to give me an error but it returned an answer.
Codx = [num for num in range(1,9) if num & 2 = 0]
Print (codx)
I got the answer
[1,4,5,8]
Then I did
Print(3&2)
Answer was 2
Print(5&2)
Answer was 0
What’s the role of the ampersand?
That's the bitwise-and operator: For the official documentation, it does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.
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.