3

I have inherited this python program and, being sort of a noob with bits and such I can't figure out what the problem is. I am getting a syntax error on the first line of the following function:

def tileKey(self, z, x, y):
    zBits = z & 0xFFL
    #  8bits, 256 levels. normally [0,21]
    xBits = x & 0xFFFFFFFL
    #  28 bits
    yBits = y & 0xFFFFFFFL
    #  28 bits
    key = (zBits << 56) | (xBits << 28) | (yBits << 0)
    #  return the key value integer 720576213915009588
    return key
Frank Conry
  • 2,694
  • 3
  • 29
  • 35

2 Answers2

12

If you're using Python 3.x, then you can't use the 'L' suffix anymore as it's no longer required and not part of the syntax:

yBits = y & 0xFFFFFFFL
Original exception was:
  File "<stdin>", line 1
    0xFFL
        ^
SyntaxError: invalid syntax
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
0

It's because of the wrong indentation, you have to indent body of the function. Your function should be indented like this:

def tileKey(self, z, x, y):
    zBits = z & 0xFFL
    #  8bits, 256 levels. normally [0,21]
    xBits = x & 0xFFFFFFFL
    #  28 bits
    yBits = y & 0xFFFFFFFL
    #  28 bits
    key = (zBits << 56) | (xBits << 28) | (yBits << 0)
    #  return the key value integer 720576213915009588
    return key

It looks like it is a method in a class, so the whole definition of the method should be indented after the line with class keyword, for example:

class YourClass:
    def tileKey(self, z, x, y):
        zBits = z & 0xFFL
        #  8bits, 256 levels. normally [0,21]
        xBits = x & 0xFFFFFFFL
        #  28 bits
        yBits = y & 0xFFFFFFFL
        #  28 bits
        key = (zBits << 56) | (xBits << 28) | (yBits << 0)
        #  return the key value integer 720576213915009588
        return key
piokuc
  • 25,594
  • 11
  • 72
  • 102