Why use hexadecimal constants?

后端 未结 11 989
挽巷
挽巷 2020-12-07 13:16

Sometimes I see Integer constants defined in hexadecimal, instead of decimal numbers. This is a small part I took from a GL10 class:

public static final int          


        
11条回答
  •  臣服心动
    2020-12-07 13:41

    "It's obviously simpler to define 2914 instead of 0x0B62"

    I don't know about that specific case, but quite often that is not true.

    Out of the two questions:

    • A) What is the bit value of 2914?
    • B) What is the bit value of 0x0B62?

    B will be answered more correctly faster by a lot of developmers. (This goes for similar questions as well)


    0x0B62 (it is 4 hex digits long so it reprensents a 16-bit number)

    • the bits of 0 = 0000
    • the bits of B = 1011
    • the bits of 6 = 0110
    • the bits of 2 = 0010

    ->

    0000101101100010

    (I dare you to do the same with 2914.)


    That is one reason for using the hex value, another is that the source of the value might use hex (the standard of a specification for example).

    Sometimes I just find it silly, as in:

    public static final int NUMBER_OF_TIMES_TO_ASK_FOR_CONFIRMATION = ...;
    

    Would almost always be silly to write in hex, I'm sure there are some cases where it wouldn't.

提交回复
热议问题