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

3 Answers3

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

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
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

Jacobjanak
  • 307
  • 1
  • 2
  • 10
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.

Bobby Ocean
  • 3,120
  • 1
  • 8
  • 15