The magic number in this case is 0x9e3779b9, which in base 10 is 2654435769. Is there any reason why the code
seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
uses the hex representation instead of the base-10 representation? Would the functionality remain identical if 2654435769 was substituted for 0x9e3779b9 in the code?
Literals are literals and different representations of the same literal are... literally identical.
However, expressions (literal or not) also have a type.
The equivalent literal would have been 2654435769u
(note the type suffix making it unsigned
).
Look at this simple test Live On Coliru
0x9e3779b9
has type unsigned int
(32 bit) and 2654435769
has type long
(64 bit)2654435769u
has type unsigned int
(32 bit) againAs you can see, the hex representation favours unsigned and the decimal representation favours signed, making the type bigger¹.
¹ native integer sizes are implementation defined
(Beyond types, one could argue that maybe, perhaps, bit-distribution is slightly more apparent in hex, octal or ultimately binary representations)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With