Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

hexadecimal in typedef enum in C

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...