Reputation: 34175
I wanted to mask out some of the bits of Python hashes and was surprised when I checked the lengths of different hashes.
>>> import sys
>>> sys.getsizeof(hash(42))
14
>>> sys.getsizeof(hash("Hello World"))
16
>>> sys.getsizeof(hash("Lorem ipsum dolor sit amet, consectetur adipisicing elit"))
18
Having a hash function returning fixed size hashes would make it easier to work with them on a bit level. Is there a reason for the variable length? Is there a guaranteed minimum length?
Upvotes: 2
Views: 574
Reputation:
>>> sys.getsizeof(2**10)
14
>>> sys.getsizeof(2**20)
16
>>> sys.getsizeof(2**30)
18
The return value of hash
is limited to some small number of bits (probably 64), but the granularity of the size of an int
is smaller than that, so depending on the exact hash value, the sizeof
can vary.
As a comment already pointed out, this has no significance for bit fiddling. For the purpose of bitwise operators, (non-negative) numbers are padded with an unlimited number of zeros.
Upvotes: 3