Why is magic number in boost :: hash_combine specified in hex

The magic number in this case is 0x9e3779b9, which in base 10 is 2654435769. Is there a reason why the code

seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); 

      

uses hexadecimal representation instead of base-10 representation? Is the functionality the same if 2654435769 was replaced with 0x9e3779b9 in the code?

+3


source to share


1 answer


Literals are literals and different representations of the same literal ... are literally identical.

However, expressions (literal or not) also have the type ...

The equivalent literal would be 2654435769u

(note the type suffix does it unsigned

).

Take a look at this simple Live On Coliru test

  • 0x9e3779b9

    is of type unsigned int

    (32 bits) and
  • 2654435769

    is of type long

    (64 bit)
  • 2654435769u

    is again of type unsigned int

    (32 bit)


As you can see, the hexadecimal representation is unsigned and the decimal representation matches the signed one, making the type larger.


ยน own integer dimensions are implementation-defined

(Aside from types, it could be argued that perhaps the bit allocation is a little more obvious in hex, octal, or ultimately binary)

+5


source







All Articles