2 ms·
If you're reading a file format that does bit packing, such as what the article is talking about, then as you read over the bytes / members of that file, you're
by deathanatos 10y ago
If you're reading a file format that does bit packing, such as what the article is talking about, then as you read over the bytes / members of that file, you're probably going to need to get those out of the resulting bytes/decoded integers with Python's bit-shifting instructions. For example, if I have an unsigned 16-bit LE in a file, where the high four bits are flags, and the low 12 bits a value of some ilk, I can parse that with something like,
packed = fileobj.read(2)
# (missing: check the read for early EOF)
packed, = struct.unpack('<H', packed)
flags = packed >> 12
value = packed & 0xfff
> there is only one type of int (64 bit)
Python's integer type isn't 64 bits. It's however many bits you need it to be. For example, I can compute 2 raised to the 128th power, without use of floating point math (and thus, get an exact answer):
In [13]: 2**128
Out[13]: 340282366920938463463374607431768211456
In [14]: type(2**128)
Out[14]: int
This computation would not fit in a 64 bit integer. (For the non-Python programmers, there is a floating-point type, "float", which in CPython corresponds to an IEEE double.)
> which is not very fast.
It's fast enough for most things. :-) There's numpy, and always C/C++/Rust if you need more speed.