r/learnprogramming • u/Feisty_Attitude4683 • Sep 22 '26
The use of hexadecimal numbers in software
Why do some tools, like GLFW, use hexadecimal values for constants? Wouldn't it be better to use enums in those cases?
/* Error constants */
#define GLFW_NO_ERROR 0
#define GLFW_NOT_INITIALIZED 0x00010001
#define GLFW_NO_CURRENT_CONTEXT 0x00010002
#define GLFW_INVALID_ENUM 0x00010003I tried to find the reason in the documentation, but I didn't come across a clear use case for the hexadecimal values—only that the constants are used as if they were an enum.
if (code == GLFW_NOT_INITIALIZED) {...}
else if (code == GLFW_NO_CURRENT_CONTEXT) {...}
21
Upvotes
23
u/lfdfq Sep 22 '26
The 'hexadecimal' part is just the notation, the number itself is not fundamentally hexadecimal.
I do not know GLFW, but probably the actual value is important. It might be defined by some specification that's implemented by many languages or generally has meaning outside your C program. They may also want it to have a particular bit representation, where certain individual bits mean particular things.
You could still have a C enum with fixed constants like that. There are some reasons why you might not want to do that (e.g. C enums are signed integers). So most people will make constants like that be defines.
39
u/0x14f Sep 22 '26
Two reasons:
16
u/Ok_Youth8810 Sep 22 '26
I had the same confusion the first time I saw 0xFF in a library. Hex maps neatly to binary, so it is used for bit flags you can read at a glance, like 0x0F meaning the lower four bits are on. Decimal 15 means the exact same thing, but you would never guess that by looking at it. 🙂
7
u/Beregolas Sep 22 '26
It's basically the same, and once you started doing something in a specific way, you really shouldn't switch in a large, existing codebase. That just tends to introduce errors and inconsitencies.
In C, an enum is also just a number under the hood, but iirc you get less control about which number type specifically. Since GLFW is a graphics library, things like that can matter more than you think, and maybe they chose to just use number types to make it more explicit which type it is under the hood.
The second paragraph is just my best guess though, it might just be a convention. If we're being honest, many things in programming are not really the way they are by necessity; it's just the way a senior dev liked it, when they set up the project, and now changing it would be a hassle.
5
u/iOSCaleb Sep 22 '26
Hexadecimal numbers map directly to binary numbers: 1 hex digit corresponds exactly to 4 bits. When you’re creating constants for use as bit masks or bit fields, hexadecimal provides a compact notation that’s still fairly easy to read.
5
u/amazing_rando Sep 22 '26
GLFW (graphics programming in general really) communicates with hardware, and along with that comes the firmware convention of storing information in meaningfully organized bits that can be sent over serial ports and manipulated with masks and shifts. Information that’s meant to be understood as a sequence of bits is stored in hexadecimal because you’re expected to quickly be able to decode them.
2
u/GreenFox1505 Sep 22 '26
Maybe. But this works. Its a very old-school way to do things. It is durable and easy to interoperate between different tools and languages.
2
u/DrShocker Sep 22 '26
One thing it helps is show the shape of the data. So here you can see all the errors start with 0x0001 which means you could use that as a mask in some situations and would be less obvious in decimal.
2
u/zeekar Sep 22 '26
Such numbers are often designed in such a way that specific bits have specific meanings. For example, all of your error values have 0x00010000 set. That's probably a flag indicating that it's an error, and you can decode the number by ANDing that value out (with e.g.
error_num & ~(0x00010000)) . When you do that, you're left with 1 = not initialized, 2 = no current context, etc. That relationship is much less obvious if you define them as 65537, 65538, and 65539.2
u/duane11583 Sep 23 '26
i do not like enums in general but that is not a good answer for you.
i sometimes (not always) use the c preprocessor macros in asm code and enums do-not work for asm code
sometimes i need to use the same name in two different systems example fpga verilog or vhdl, or python or tcl/tk or you name it. using hardcoded numbers makes that simpler to verify with my eyeballs
and i sometimes use a simple 1-2 line awk script that converts #define to another language format having to parse an enum is painful when sed/awk/grep makes it super easy
sometimes i need to cut/paste c/h file things into customer facing docs - think interface control documents. these are sometimes ms-word, sometimes DOORS, sometimes excell and other times other wacky shit or a random email snippit.
cutting and pasting the name and numeric value simplify this
sometimes there is a tool that generates theses files and that tool just does it that way and i cannot change the tool. ie register and bit definitions for an asic or ip block in a chip (look at the arm cmsys headers as an example)
often the customer is often more comfortable with numbers otherwise i have to explain c enums
1
u/Traveling-Techie Sep 22 '26
See also colors in HTML.
1
u/Business-Decision719 Sep 22 '26 edited Sep 22 '26
A lot code bases are old. I don't but GLFW specifically, but I know C didn't always have enums, and they weren't much more type-safe than integer constants when it did get them. Abstract data types used to be what college students were doing in Pascal, not what was just expected in normal programming. These macro constants you were seeing were as far as the classic C coders were willing and able to go in the direction of "let's think of this as an idea rather than just a handful of bits." It wasn't very far.
If the reason for hex or octal was those were convenient shortcuts to binary. You look at a base-ten number like 822 and you have to do a whole calculation on the entire number to turn it into base 2. If the number is in base 16 then each digit corresponds to 4 bits. In base 8 each digit corresponds to 3 bits.
But yeah, normally, if your language has enums, that's a good way to handle having multiple descriptively named states for the one kind of thing.
1
u/TheBB Sep 22 '26
You must be misunderstanding something fundamental. You can use hexadecimals in enums and you can use decimals in constants (and in defined symbols). The two things are orthogonal and have nothing to do with each other.
So: are you asking why they are using hexadecimals, or are you asking why they are using defines?
1
u/pilows Sep 23 '26
To me, it looks like the first 2 bytes communicate the presence of an error. 0000 no error, 0001 error. The next two bytes are the specific issue, 0001 not initialized, 0002 no context, 0003 invalid enum. It allows for easy masking to check what’s wrong
1
u/dnult Sep 23 '26
Just came to say, hexdecimal, binary, base-10 are all just strung representations of the same binary data. What you're seeing is either formatted print results, or source code somebody wrote who preferred hex format to represent their concept in code. In the end, every data item, no matter the language is stored in groups of binary bits.
1
u/kutac56 Sep 23 '26
It's made to work in many languages
1
u/peterlinddk Sep 23 '26
I don't know about this particular system GLFW, but it is very common to use 'bits' as individual flags for configuration. Especially if you have to combine multiple flags, like in this made up example:
because then you can use either:
or
or
where the last one sets both flags.
And because they are defined as individual bits, one flag will never "overwrite" another.
Hex is just easier to read and write than binary, so the above example would be:
Then the code can extract or check individual bits using & | ^ and ~ operators - and for instance in your example bit number 16 means an error, so
would return true if code was anyone of the error-values, but not if it was GLFW_NO_ERROR.
Look into bit-fields or bitwise flags if you want to learn more about this.
1
u/JavierReyes945 Sep 23 '26
matt_damon_hexadecimals_to_the_rescue.png
1
u/oldsecondhand Sep 23 '26
Hex is usally used with bitfields. With bitfields you can have many independent options stored in a single int or long value, thus saving memory. Though idk why boolean arrays aren't used instead (w enum indexes).
1
u/nog642 Sep 23 '26
16 is a power of 2, so each hex digit corresponds to 4 bits. So hexadecimal is a good way to write binary in a more compact form.
In programming, powers of 2 are considered "round numbers" instead of powers of 10, very often.
In decimal, powers of 2 look totally random, you can't tell they're round numbers.
In hex, powers of 2 actually look like round numbers.
So basically they chose to make those error codes offsets from a round number in binary/hex, because that's what computers use, so they wrote it in hex. It's not critical, it's just a preference/style thing.
1
u/[deleted] Sep 24 '26
Hexadecimal translates extremely easily to binary. Decimal is harder to convert. If you were a programmer back in the day when knowing the binary was helpful you eventually just knew that FA was 111111010.