-1
>>> 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.

1
  • 0111 is not a valid integer literal. What did you intend to create? Commented Apr 12, 2021 at 0:17

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well, they are still int objects
@juanpa.arrivillaga true, that was a bit imprecise.
@AdamSmith I don't know if that pun was intentional but it was hilarious either way!
1

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

1 Comment

You can specify binary literals in Python3 (and Python2) with a leading 0b. assert 0b111 == 7
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.