8

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?

HamZa
  • 14,671
  • 11
  • 54
  • 75
1110101001
  • 4,662
  • 7
  • 26
  • 48

1 Answers1

8

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) again

As 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)

sehe
  • 374,641
  • 47
  • 450
  • 633