I am struggling to understand what the &^ and &^= operators mean in Go. I cannot find an answer either in the documentation (which states it's a bit clear operator, but it does not help me much) or by trials.
In particular, I want to know if there is an equivalent in Python.
These are the "AND NOT" or "bit clear" operators, "useful" for clearing those bits of the left-hand side operand that are set in right-side operand.
I put the "useful" in quotes since all other languages that derive the bitwise operations from C one does this with bitwise AND & and bitwise NOT ~; thus 5 &^ 2 would be just 5 & ~2 in Python; and a &^= 3 of Go would be a &= ~3 in Python.
Related
Just to check, I couldn't find anything about ~, so I want to make sure it's the same.
Also, can you use ! in python like in C++?
It seems that these two codes give the same output:
bool(~0)
bool(not 0)
This is how it is in Python.
'~' is a bitwise operator
whereas 'not' is a logical opeartor
You should read Python 3.10 documentation at Unary arithmetic and bitwise operations.
The ~ operator does a simple job:
Inverts the bits
This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 8 years ago.
In Python
>>> i = 3
>>> -i**4
-81
Why is -i**4 not evaluated as (-i)**4, but as -(i**4)?
I suppose one could argue that raising to a power takes precedence over (implicit) multiplication of i with minus one (i.e. you should read -1*i**4).
But where I learned math, -i**n with n even and i positive, should come out positive.
The ** operator binds more tightly than the - operator does in Python. If you want to override that, you'd use parentheses, e.g. (-i)**4.
https://docs.python.org/2/reference/expressions.html#operator-precedence
https://docs.python.org/3/reference/expressions.html#operator-precedence
You can use the pow() function from math.
import math
i = 3
math.pow(-i,4)
This will yield a positive value.
As stated here: Exponentials in python x.**y vs math.pow(x, y), this option (or just build in pow()) would be ideal if you want to always produce a float.
The power operator (**) has a higher precedence than the unary negation (-) operator. -i**4 is evaluated as -(i**4) - i.e., you take 3 up to the power of four, which is 81, and then negate it, resulting in -81.
You have to do (-i)**4 to get a positive result.
The *'s have higher priority than the '-'.
When in doubt, use parentheses. (or, as Amber suggested, refer to the language documentation)
I can't find in PEPs information about style of bitwise operators (|, &), in this code in particular:
class MySplashScreen(wx.SplashScreen):
def __init__(self, parent=None):
wx.SplashScreen.__init__(self, logo.getBitmap(), wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, parent)
Should I use spaces in this case (wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT)?
I would definitely use spaces on either side. Otherwise, it'd hard to spot the | in between the variable/constant names.
The place to find this, if it existed, would be Whitespace in Expressions in PEP 8. However, these operators are not mentioned:
Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
I think there's a good reason for this. While you almost certainly want the space in wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, I'm not sure you want it in a|b. In fact, an expression like a&b | c&d seems a pretty good parallel to the recommended x*x + y*y.
The reason you want it here has nothing to do with the operator being |, but with the value being wx.SPLASH_CENTRE_ON_SCREEN. In fact, I think you'd make the same decision with BIG_LONG_CONSTANT_1 + BIG_LONG_CONSTANT_2. So, maybe there should be an additional rule in the style guide about whitespace around operators when the operands are ugly capitalized constants.
But meanwhile, I don't think there is, or needs to be, a specific rule about the bitwise operators. Treat them the same way you do the arithmetic operators. (And note that there is no specific rule for whether or not to put spaces around, e.g., +, except in the case where operators with different priorities are used in the same expression. In fact, you see it both ways within PEP8 itself. That implies that it's acceptable either way in general, and you have to use your own judgment in specific cases.)
All that said, the style checker pep8 flags both bitwise and arithmetic operators without whitespace with an E225. And it even flags the "different priorities" examples like x = x/2 - 1 (which PEP 8 lists as "good") with the optional E226 warning. See missing_whitespace_around_operator for details. I don't think this counts as any kind of official endorsement—but I think "I put the spaces here so the code would pass the style checker we've chosen to use for this project" is a pretty valid reason. (But you might want to check alternatives like pep8ify, and see if pylint, pyflakes, etc. have anything to say on the topic.)
I've been learning Python but I'm a little confused. Online instructors tell me to use the operator ** as opposed to ^ when I'm trying to raise to a certain number. Example:
print 8^3
Gives an output of 11. But what I'm look for (I'm told) is more akin to: print 8**3 which gives the correct answer of 512. But why?
Can someone explain this to me? Why is it that 8^3 does not equal 512 as it is the correct answer? In what instance would 11 (the result of 8^3)?
I did try to search SO but I'm only seeing information concerning getting a modulus when dividing.
Operator ^ is a bitwise operator, which does bitwise exclusive or.
The power operator is **, like 8**3 which equals to 512.
The symbols represent different operators.
The ^ represents the bitwise exclusive or (XOR).
Each bit of the output is the same as the corresponding bit in x if
that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.
** represents the power operator. That's just the way that the language is structured.
It's just that ^ does not mean "exponent" in Python. It means "bitwise XOR". See the documentation.
I've got the IF statement;
if contactstring == "['Practice Address Not Available']" | contactstring == "['']":
I'm not sure what is going wrong(possibly the " ' "s?) but I keep getting the error mentioned in the title.
I've looked in other questions for answers but all of them seem to be about using mathematical operates on strings which is not the case here. I know this question is kind of lazy but I've been coding all day and I'm exhausted, I just want to get this over with quickly.(Python newb)
| is a bitwise or operator in Python, and has precedence so that Python parses this as:
if contactstring == (""['Practice Address Not Available']"" | contactstring) == "['']":
Which generates the error you see.
It seems what you want is a logical or operator, which is spelled 'or' in Python:
if contactstring == ""['Practice Address Not Available']"" or contactstring == "['']":
Will do what you expect. However, since you're comparing the same variable against a range of values, this is even better:
if contactstring in ("['Practice Address Not Available']", ['']):
The | is a bitwise operator which doesn't work on strings...
Using or (a boolean logic operator) will yield better results.
The problem here is the bitwise-or operator |. In a Boolean context that would normally work OK, but | has higher precedence than == so Python is trying to evaluate "['Practice Address Not Available']" | contactstring first. Both of those operands are strings, and you can't bitwise-or two strings. Using the more correct or avoids this problem since it's lower precedence than ==.