Hello! Quite often in open-source projects I notice that developers write numbers not like this:

int i = 2147483647; 

And so:

 int i = 0x7FFFFFFF; 

It gives the same result: we have a variable containing the value = int.MaxValue, but what is the deep logic of such a record? Simply, they say: "I can write numbers in hexadecimal representation, look at me!" Or are there some advantages? Maybe the assignment goes on nanoseconds faster and / or the machine is easier to perceive them? Please resolve my doubts about this!

    2 answers 2

    Obviously, this initializer value

     int i = 2147483647; 

    difficult to remember. You need to constantly check the directory to correctly write out all the numbers.

    Such a record

     int i = 0x7FFFFFFF; 

    does not require a phenomenal memory.

    But in any case it would be easier, better and clearer to write

     int i = int.MaxValue; 

    since such a record is self-documenting.

    Record

     int i = 0x7FFFFFFF; 

    it is preferable to the previous record only in one case: when the variable i used as a certain mask for bit operations.

    • You can also add that with the first version of the record, you can make a mistake in one digit in the middle, and no one will notice. In the second variant it is enough to check the number F after the seven) - insolor
    • @insolor In principle, I talk about it. - Vlad from Moscow

    It is often necessary to write a binary unsigned integer. Each hexadecimal digit can replace 4 bits in a binary representation, i.e. a byte is two concatenated hexadecimal digits. It is easier to read and write than rows of binary digits.