Sorry your code is even unclearer than that in the article Where did you pull the 18 from for example Non experts of Python won t know that the return format of bin a will have a 0b prefix Note that Python is used here as pseudocode so it s better to avoid assuming much knowledge of the Python standard library The Python code in the article suffers from two problems 1 It s unclear what kind of value each variable holds The variables should either be renamed to something self descriptive or a comment should explain the role of each 2 The code is slightly obscure because it tries to be smart about not recomputing an index expression Now to explain the algorithm we don t need optimized code and an optimizing compiler which we d use if efficiency is of any concern would spot this anyway so there isn t any reason to even slightly complicate the code even in production The code from the ACM Sian article by Chan is much clearer here s transcribed and commented pseudocode def z order p q d Returns true if p q according to Z order in d dimensions p and q are supposed to be arrays of d coordinates i 0 index of most significant dimension for j in range 1 d Use exclusive or to eliminate equal bits msb is less will see ones where the coordinates differ if msb is less p i q i p j q j i j return p i q i def msb is less l r Returns true if the Most Significant Bit of l is less than that of r return l r or l l r Described as neat trick by Chan Note that the algorithm in the article has l r instead of l r this changes the result of msb is less but I suspect it does not change the result of z order