Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was searching for the reason behind using hexadecimal in typedef enum in C.

I followed the following link, but there are two answers: c, obj c enum without tag or identifier

LearnCocos2D says that, "there's no gain to use hex numbers and in particular there's no point in starting the hex numbers with a through f (10 to 15). "

Sulthan says that, "Hexadecimal numbers are commonly used when the integer is a binary mask". I searched for binary mask and came to understand that, its a technique used in bitmap gaming from following link https://en.wikipedia.org/wiki/Mask_(computing)

If sulthan is right, kindly help me understand it.

I don't have enough reputation to comment, so I created this as new question.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
350 views
Welcome To Ask or Share your Answers For Others

1 Answer

For a bit mask it helps to look at values in binary since that is the level needed for a bit mask. And each enum value typically only sets a single bit.

So the enum values would be set (in binary) to 00001, 00010, 00100, 01000, 10000, etc.

Those same values in decimal would be: 1, 2, 4, 8, 16, etc.

And in hex they would be 0x01, 0x02, 0x04, 0x08, 0x10, etc.

It's really just a matter of preference but since hexadecimal is a power of 2, it better relates to binary than decimal does. This makes it slightly clearer that the values represent bit mask values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...