1

Recently, Google published some hash functions on Github. On this functions codes, some values are defined on base 16, for example on scalar_highway_tree_hash.cc we have:

 const Lanes init0 = {0xdbe6d5d5fe4cce2full, 0xa4093822299f31d0ull,
                     0x13198a2e03707344ull, 0x243f6a8885a308d3ull};

0xdbe6d5d5fe4cce2full is a hexadecimal unsigned long long integer that is equal to 15845587454020866000 on base 10.

Now i have this question: Why we use this notation instead of writing value in direct decimal representation? Expect the shorter string, is there any other reason for using this manner?

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
meysam
  • 1,754
  • 2
  • 20
  • 30
  • 2
    What is *"direct"* about decimal? It's really a *binary* somewhere. – jonrsharpe Mar 20 '16 at 13:46
  • 3
    Each hexadecimal digit represents the same number of bits. Might make it easier to "see" the binary result. – Bo Persson Mar 20 '16 at 13:47
  • 2
    If you've got a value that you're using as a bitmask it's often much easier to see which bits that are set in hexadecimal notation compared to decimal notation. E.g. 0xF0FF vs 61695. – Michael Mar 20 '16 at 13:47
  • 1
    [Why do Computers use Hex Number System at assembly language?](http://stackoverflow.com/q/24198530/995714), [Importance of Hexadecimal numbers in Computer Science](http://stackoverflow.com/q/16513806/995714), [Why use hex values instead of normal base 10 numbers?](http://stackoverflow.com/q/451886/995714), [Why are flag enums usually defined with hexadecimal values](http://stackoverflow.com/q/13222671/995714) – phuclv Mar 20 '16 at 13:51
  • Hex (base 16) is a multiple of 8. Thus two hex digits represent one byte. As others allready said its easier to see the bitmask this way. – Mario Dekena Mar 20 '16 at 13:52
  • I know that there are only different representation of one thing, but I want to know situations of using each representation. @BoPersson – meysam Mar 20 '16 at 14:31
  • Links helped me, thanks. @LưuVĩnhPhúc – meysam Mar 20 '16 at 14:39
  • @MarioDeken *Non sequitur.* Octal is also a multiple of 8 but it doesn't have that property. – user207421 Mar 24 '16 at 04:54
  • @EJP Well that was not the exact relation. To be correct: 8 is a multiple of log2(16). log2(16) equals 4. This means one hex digit represents 4 binary digits. Thus, you need 8/4=2 hex digits for one byte. For octal: log2(8) equals 3. Its still an even number meaning you can directly read octal as binary as well (one octal digit equals 3 binary digits). But since 3 is not a multiple of 8 you cant fill a byte. In general: A number with base a can fully represent n digits in base b if logb(a) | n is true. Quod erat demonstrandum :D – Mario Dekena Mar 24 '16 at 16:22

0 Answers0