The Beginner's Guide to Using Enum Flags
2011-06-11 15:53
561 查看
The Beginner's Guide to Using Enum Flags
原文Introduction
Once I was roaming the Visual C++ forum (again), I had to face the fact that bitwise operations, and binary in general, are rarely in beginner's common sense. After having pained my fingers to write a very long answer to that innocent person, it became obvious that I had to share this obfuscated knowledge with the community through this article.This is obviously a beginners article, but if you want to get deeper knowledge about the C/C++ bitwise operators cover, you can read the very complete article, An introduction to bitwise operators by PJ Arends. You can also go into a very complex (but effective) analysis of bit hacking with the article, Bit Twiddling Hacks By Sean Eron Anderson.
I'm going to present in the most complete way that I can about what we can do with bitwise operators, flags and all that binary stuff.
The Facts
One of the places where we most find the use for this is certainly when a library provides a set of enumerations and when functions useDWORDas a flags container. Let's take for that article the example of an
enumwhich defines some styles:
Collapse enum { STYLE1 = 1, STYLE2 = 2, STYLE3 = 4, STYLE4 = 8, STYLE5 = 16, STYLE6 = 32, STYLE7 = 64, STYLE8 = 128 }; | OR | Collapse enum { STYLE1 = 0x1, STYLE2 = 0x2, STYLE3 = 0x4, STYLE4 = 0x8, STYLE5 = 0x10, STYLE6 = 0x20, STYLE7 = 0x40, STYLE8 = 0x80 }; |
Collapse
1 -> 0b 00000000 00000000 00000000 00000001 2 -> 0b 00000000 00000000 00000000 00000010 4 -> 0b 00000000 00000000 00000000 00000100 8 -> 0b 00000000 00000000 00000000 00001000 16 -> 0b 00000000 00000000 00000000 00010000 32 -> 0b 00000000 00000000 00000000 00100000 64 -> 0b 00000000 00000000 00000000 01000000 128 -> 0b 00000000 00000000 00000000 10000000
Notice that for all of these values, only one
bitis set at a time, all the others are equal to
0. You now can see a high level of interest appearing in this, which is that each bit is used as a flag for a functionality (here, each bit represents a style). We can now imagine a way to mix the flags together in one variable, to avoid using as many booleans as we need flags. Consider the following example :
Collapse
0b 00000000 00000000 00000000 00100101 Flags of Style1, Style3 and Style6 are set
The Main Operators
We face a problem now. C++ doesn't handle binary directly. We have to usebitwise operatorsinstead. There are 3 atomic bitwise operators to know, presented by ascending order of priority : OR (
|), AND (
&) and NOT (
~). Here are their behaviors:
Collapse
x y | x y & x ~ --------- --------- ------- 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 1 1 1 1 1
Knowing this, we can use these operators to build some mixes such as the ones presented above.
In the case of the instruction
STYLE1 | STYLE3 | STYLE6, we
ORthe constants like this:
Collapse
0b 00000000 00000000 00000000 00000001 <- STYLE1 0b 00000000 00000000 00000000 00000100 <- STYLE3 0b 00000000 00000000 00000000 00100000 <- STYLE6 ----------------------------------------------- 0b 00000000 00000000 00000000 00100101 <- STYLE1 | STYLE3 | STYLE6
We can see that the bitwise
ORoperator is pretty much like the addition (+) operator. However, you have to be very careful if you wished to use + instead or |. The reason is simple: adding
1 + 1resolves into
0(plus one carry over
1). You won't see any problem if all the constants are strictly different though, but I won't go deeper as it is a bad practice to use other than bitwise operators when processing binary operations.
DWORD in Action
Commonly, such mixes stick to theDWORDtype. However, this is not a must as we can do this with any integer types (
char,
short,
int,
long...). A
DWORDis an unsigned 32 bits integer (like those used in the binary representations of this article). Let's imagine the case when we have such a
DWORDconstructed in a function, and passed to another in parameter. How can the called function know which bits are set, and which are not? Easy... Follow me!
Say we want to know if the bit of
STYLE8is set in the
DWORDpassed in parameter. We must have a mask which we will call the
ANDparameter. In practice, the mask is the same as the constant we are about to test, so there's no additional code to create such a mask:
Collapse
DWORD parameter -> 0b 00000000 00000000 00000000 00100101 STYLE8 mask -> 0b 00000000 00000000 00000000 10000000 ---------------------------------------- Bitwise AND -> 0b 00000000 00000000 00000000 00000000 <- 0x00000000 DWORD parameter -> 0b 00000000 00000000 00000000 10100101 STYLE8 mask -> 0b 00000000 00000000 00000000 10000000 ---------------------------------------- Bitwise AND -> 0b 00000000 00000000 00000000 10000000 <- STYLE8
If the
ANDoperation returns
0, then, the bits were not set, otherwise, you get the mask you applied.
OK, now, in practice, here is how you'll often see it:
Collapse
void SetStyles(DWORD dwStyles) { if ((STYLE1 & dwStyles) == STYLE1) { //Apply style 1 b4df } else if ((STYLE2 & dwStyles) == STYLE2) { //Apply style 2 } else if ((STYLE3 & dwStyles) == STYLE3) { //Apply style 3 } //etc... }
I didn't present the third operator NOT (
~) yet. It is generally used when you have a set of bits, in which some are set and some aren't, and where you'd like to remove one of them. The sample of code below exposes how this can be done:
Collapse
void RemoveStyle5 (DWORD& dwStyles) { if ((STYLE5 & dwStyles) == STYLE5) { dwStyles = dwStyles & ~STYLE5; } }
I didn't mention the XOR (
^) operator yet. The reason why it comes last is for the only reason that this operator is not atomic; that means that we can reproduce its behavior with the other operators presented already:
Collapse
#define XOR(a,b) (((a) & ~(b)) | ((b) & ~(a)))
Anyway, this operator can be used to easily switch one bit :
Collapse
void SwitchStyle5 (DWORD& dwStyles) { dwStyles = dwStyles ^ STYLE5; }
The Other Operators
Now, to perfect your sharpen skills on bits handling, there are few other operators you have to know to be unbeatable: the shift operators. Those can be recognized like this :<<(left shifting, follow the arrows),
>>(guess what, this is a right shifting).
What a shifting operator is moving the bits of n positions to the right or to the left. The "space" made by the movement is padded with zeros, and the bits pushed over the limit of the memory area are lost.
Let's take an example:
Collapse
BYTE dwStyle = STYLE1 | STYLE3 | STYLE6; // [00100101]000 <- dwStyle = dwStyle << 3; // 001[00101000] BYTE dwStyle = STYLE1 | STYLE3 | STYLE6; // 000[00100101] -> dwStyle = dwStyle >> 3; // [00000100]101
"But" I hear you say, "what is that for" ?". Well, there are tons of applications which I don't particularly have in mind right now, but trust me, when you need it, be happy it's there.
Anyway, two funny uses of such operators I can think of is the division and the multiplication of an integer by 2. Check this:
Collapse
char i = 127; // 0b01111111 ( = 127) i = i >> 1; // 0b00111111 ( = 63) i = i << 1; // 0b01111110 ( = 126)
Shifting one bit right, then one bit left gives a different result from the beginning. That's because as we just saw, the "overflown" bits are lost, and the newly inserted ones are set to 0. But let's look closer. This is working anyway because we are working on integers. That is, dividing an odd number will not give a float (we don't care about the remaining 0.5).
Here,
127 / 2should be
63.5, so it gives
63, due to the truncation.
63 * 2 = 126, isn't it what we want ?! :-)
Now we've seen all the useful operators, just know that all of them have their own assignment operator.
Collapse
dwStyle &= 0x2 --> dwStyle = dwStyle & 0x2 dwStyle |= 0x2 --> dwStyle = dwStyle | 0x2 dwStyle ^= 0x2 --> dwStyle = dwStyle ^ 0x2 dwStyle <<= 0x2 --> dwStyle = dwStyle << 0x2 dwStyle >>= 0x2 --> dwStyle = dwStyle >> 0x2
A Funny Example
Thanks to Randor in the VC++ forum, here is a nice example of what is possible to do with such operators (Credit for the discovery of this little neat trick below goes to Sean Anderson at stanford university)."How is it possible to reverse the bits of an integer, say from 11010001 to 10001011 ?"
Collapse
BYTE b = 139; // 0b10001011 b = ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
"Wowwww, how is this working man ?!"
Collapse
DWORD iTmp = 0; BYTE b = 139; // 00000000 00000000 00000000 10001011 DWORD c = (b * 0x0802LU); // 00000000 00000100 01011001 00010110 c &= 0x22110LU; // 00000000 00000000 00000001 00010000 DWORD d = (b * 0x8020LU); // 00000000 01000101 10010001 01100000 d &= 0x88440LU; // 00000000 00000000 10000000 01000000 iTmp = (c | d); // 00000000 00000000 10000001 01010000 iTmp = iTmp * 0x10101LU; // 10000001 11010001 11010001 01010000 iTmp >>= 16; // 00000000 00000000 10000001 11010001 b = iTmp; // 00000000 00000000 00000000 11010001
Conclusion
Well, here we are then. If you came here to understand these binary operations, I hope that I helped you in your way. If you came to see what was going on here, then don't hesitate to point my mistakes if any, so that I can fix them.Links
The following links are for complimentary knowledge, if you like to go deeper in bitwise handling:An Introduction to Bitwise Operators by PJ Arends
Bit Twiddling Hacks by Sean Eron Anderson
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)About the Author
相关文章推荐
- 转-Quick guide to setting up SSL using Domino as the Certificate Authority
- The Beginner’s Guide to iptables, the Linux Firewall
- A Beginner’s Guide to npm — the Node Package Manager
- The Absolute Beginner’s Guide to Node.js( Node.js 新手指南 (Manuel Weiss))
- This page needs work The Complete Blanks Guide To Using The OGRE SDK AppWizard
- A quick guide to writing scripts using the bash shell
- The Absolute Beginner’s Guide to Node.js( Node.js 新手指南 (Manuel Weiss))
- Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft(R) E
- The Beginner's Guide to Broadband and Wireless Internet
- FileMaker Web Publishing: A Complete Guide to Using the API for PHP
- The Beginner’s Guide to iptables, the Linux Firewall
- The Definitive Guide to Using Negative Margins
- How to cast enums which are using QFlags and QList?
- The Scientist and Engineer's Guide to Digital Signal Processing一书的阅读总结
- The Definitive Guide to Grails, Second Edition
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
- UMTS Performance Measurement: A Practical Guide to KPIs for the UTRAN Environment
- #715 – 在Canvas中使用Thumb控件实现拖动目标的功能(Using the Thumb Control to Drag Objects on a Canvas)
- How to capture video frames from the camera as images using AV Foundation
- The Ultimate Guide To iPhone Resolutions