>>> x = 1101 ^ 0111
File "<stdin>", line 1
x = 1101 ^ 0111
^
SyntaxError: invalid token
Why am I getting this syntax error in python? I see online that, "^ Bitwise Exclusive XOR Description Returns the result of bitwise XOR of two integers.
Syntax A ^ B
A Integer object. B Integer object."
So I think I am using two integers.
are 1101
and 0111
supposed to be bits? To represent bit literals, you should use 0b1101
and 0b0111
, because otherwise those are integers (and ints can't start with a 0
First, you cannot use integers in such a way. Here is the error I got when I ran your code:
SyntaxError: leading zeros in decimal integer literals are not permitted;
use an 0o prefix for octal integers
In other words, you can't give Python an integer that starts with a zero. That used to work in Python 2 but is no longer supported in Python 3. (see https://stackoverflow.com/a/11620174/7583007 )
I am assuming you are trying to use binary numbers? If so, you should try this: https://stackoverflow.com/a/19414115/7583007
I believe you wanted:
0b1101 ^ 0b0111
In general, the error you are receiving is because you placed a "0" in front of a number, something that python doesn't allow. The same would happen if you tried to do:
078
The start of a number beginning with zero, typically is special python code that indicates you will be providing a binary number, octal number, or hexidecimal number, which begin with 0b, 0o, or 0x, respectively.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.