@n1k31t4 already provided great answer albeit I still have confusionquestions after reading. I work out the rest and share here.
For the first question
What is the 3rd line doing: crc32(np.int64(identifier)) & 0xffffffff < test_ratio * 2**32
We first ignore the part & 0xffffffff
because
from the doc of the package https://docs.python.org/3/library/zlib.html
Changed in version 3.0: Always returns an unsigned value. To generate the same numeric value across all Python versions and platforms, use adler32(data) & 0xffffffff.
so if you are using the 3.0 version you can ignore this part and
crc32(np.int64(identifier)) < test_ratio * 2**32
still works.
To understand why the above works, it just takes the fact that crc32 is evenly distributed across unsigned int32 https://stackoverflow.com/questions/38315172/distribution-of-crc-checksums and hence if the sample size is large enough, then test_ratio * sample_size amount of sample points would be smaller than test_ratio * $2^{32}$.
And if you are still curious what & 0xffffffff
is doing, it is mapping signed int32 to unsigned int32 (negative mapped to positive, while non-negative unchanged). 0xffffffff is a hexadecimal representation of 0b11111111111111111111111111111111 (thirty two of ones). So if you do a bitwise & with it, you will get the following:
>>> print(-1 & 0xFFFFFFFF)
4294967295
>>> print(-1 & 0b11111111111111111111111111111111)
4294967295
To understand the bitwise operation, you will need to look at python's implementation of two's complement.