I'm trying to understand a constant 0x9e3779b9.
What kind of data is this? It's not binary, not decimal, what is this?
It's a constant used on the TEA algorithm. It says it's derived from the golden number, but the golden number is 1.618?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.Sign up to join this community
As others have said, the constant is an integer in hexadecimal form. Specifically, it is a 32-bit integer in hexadecimal form. If the constant is a signed integer, then 0x9e3779b9 is negative 1640531527 decimal in two's complement form; therefore, it may be a scaled integer fraction that has been adjusted to deal with non-integral of 2 related problems.
Two's complement negative to positive conversion in hex
0x9e3779b9 ⊕ 0xffffffff + 0x00000001 = 0x61C88647 = 1640531527 in decimal
or using the 1's complement operator ~ in the C family of languages
~0x9e3779b9 + 0x00000001 = 0x61C88647 = 1640531527 in decimal
Two's complement negative to positive conversion in binary
10011110001101110111100110111001 ⊕ 11111111111111111111111111111111 + 00000000000000000000000000000001 = 01100001110010001000011001000111 = 1640531527 in decimal
I think this StackOverflow question answers it:
Essentially, it is a magic number, derived from the Golden Ratio irrational number, using the steps:
phi = (1 + sqrt(5)) / 2 = 1,6180339887498948482045868343656.
Then after it, it is calculated
2^32 / phi which results in
2 654 435 769,4972302964775847707926.
Truncate it to have only its integer part
2 654 435 769
Convert it to hexadecimal and you will get
9E37 79B9 (On Windows Calculator choose
Being it a pre-calculated integer, instead of a taking a float every time and do the computation, you accelerate the calculus of each hash done after.
0x is for a hexadecimal number, or base 16. The benefit of a base 16 number is that each pair of digits represents one byte exactly. Given a little practice, you can almost see the bit pattern in your mind, assuming you've ever worked with binary numbers.
The number comes from the hexadecimal representation of the golden ratio.
25/100) in decimal is
4/16) in hex, the fractional portion of the golden ratio has a different representation in hex than in decimal.
You are not dividing
0x9e3779b9/0x100000000 = 2654435769/4294967296 ≈ 0.6180339886
As others pointed out here and on https://stackoverflow.com/questions/4948780/magic-numbers-in-boosthash-combine, the number is indeed constructed from the golden ratio.
But there is another important reason why numbers such as pi, phi or e are used in cryptographic functions. Of course, in principle, any "reasonably random" bit sequence could be used as a constant. But it puts the creator of the function under suspicion of having engineered the value, e.g. in order to create a particular weakness in the algorithm (known only to him) that he can then exploit when you use his function.
By constructing the value from well-known "natural" constants, this kind of suspicion can be averted to some degree.