python - Explain the code of Z-order curve -


in explanation of z-order curve in wikipedia, there python code:

def less_msb(x, y):     return x < y , x < (x ^ y) 

two questions here:

  1. what msb short ?

  2. since x < y, why should compare x , (x^y) still ?

msb most significant bit

one way determine whether significant smaller compare floor of base-2 logarithm of each point. turns out following operation equivalent, , requires exclusive or operations:

def less_msb(x, y):    return x < y , x < (x ^ y) 

the second comparison needed because, if x < y, x's msb isn't less y's msb:

for example x = 2, y = 3: x < y x , y have same msb:

print bin(2), bin(3) 0b10 0b11 

you can see on table below x^y not less x until y's bit_length greater x's, until point msbs equal:

(2, 3)   2^3 =  1   bin(2):   10    bin(3):    11 (2, 4)   2^4 =  6   bin(2):   10    bin(4):   100  (3, 4)   3^4 =  7   bin(3):   11    bin(4):   100  (4, 5)   4^5 =  1   bin(4):  100    bin(5):   101 (4, 6)   4^6 =  2   bin(4):  100    bin(6):   110 (4, 7)   4^7 =  3   bin(4):  100    bin(7):   111 (4, 8)   4^8 = 12   bin(4):  100    bin(8):  1000  (5, 6)   5^6 =  3   bin(5):  101    bin(6):   110 (5, 7)   5^7 =  2   bin(5):  101    bin(7):   111 (5, 8)   5^8 = 13   bin(5):  101    bin(8):  1000  (6, 7)   6^7 =  1   bin(6):  110    bin(7):   111 (6, 8)   6^8 = 14   bin(6):  110    bin(8):  1000  (7, 8)   7^8 = 15   bin(7):  111    bin(8):  1000  (8, 9)   8^9 =  1   bin(8): 1000    bin(9):  1001 (8,10)  8^10 =  2   bin(8): 1000   bin(10):  1010 (8,11)  8^11 =  3   bin(8): 1000   bin(11):  1011 (8,12)  8^12 =  4   bin(8): 1000   bin(12):  1100 (8,13)  8^13 =  5   bin(8): 1000   bin(13):  1101 (8,14)  8^14 =  6   bin(8): 1000   bin(14):  1110 (8,15)  8^15 =  7   bin(8): 1000   bin(15):  1111 (8,16)  8^16 = 24   bin(8): 1000   bin(16): 10000 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -