Why use hex?

前端 未结 12 2026
你的背包
你的背包 2020-11-28 03:45

Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html

I noticed that in some situations they used hex numbers, like i

12条回答
  •  感动是毒
    2020-11-28 03:48

    Sometimes the visual representation of values in HEX makes code more readable or understandable. For instance bitmasking or use of bits becomes non-obvious when looking at decimal representations of numbers.

    This can sometimes do with the amount of space a particular value type has to offer, so that may also play a role.

    A typical example might be in a binary setting, so instead of using decimal to show some values, we use binary.

    let's say an object had a non-exclusive set of properties that had values of either on or off (3 of them) - one way to represent the state of those properties is with 3 bits.

    valid representations are 0 through 7 in decimal, but that is not so obvious. more obvious is the binary representation:

    000, 001, 010, 011, 100, 101, 110, 111

    Also, some people are just very comfortable with hex. Note also that hard-coded magic numbers are just that and it is not all that important no matter numbering system to use

    I hope that helps.

提交回复
热议问题