C enum bitwise operations

C enum bitwise operations

Posted: sapan Date: 03.06.2017

Welcome to my blog! All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer. Two posts ago, I talked about the C enum and some of its pitfalls here. This post continues with a discussion of the fundamentals of enum s by continuing with using enum s and bit-flags.

But they can also be used to store a combination of discrete values. That is, the standard use of an enumeration is to support mutex options - such as an order can be a New or Modify but not both. But it is not limited to that, you can form a value that is a combination of enumeration values if the values are structured in the right way - for example, a file can be opened for Read access, Write access, or both.

You may notice we decorated our enum with a [Flags] attribute. So now, this definition of a [Flags] enum will allow us to have a MessagingProperties value that is both Durable and Persistent, or is both Buffered and Durable, or is all three, or is none of the three, and so on and so on. But how would we represent this in code?

c enum bitwise operations

Note that we use the bitwise OR ' ' and not the logical OR ' '. Remember that the binary OR operator will combine the bits of both numbers such that the resulting bit is 1 if either source bit is 1. Notice that in the example above, every place where either operand had a 1 bit, the result bit is also 1.

The only place where the result is a 0 bit is where both source bits were 0. This is also why we use the binary OR instead of addition. Notice the results are not the same!

This is why we always use bitwise OR for safety. So now, if we combine them using OR like above, we get:. Thus, both bits have been set for each value. It might be tempting to think of this as an AND instead of an OR, but you must keep in mind the binary math. Combining in binary math is an OR operation, not an AND operation. Yes, remember that the bitwise and sets a resulting bit to 1 only if BOTH source bits are 1. Remember I said that the default numbering of enum values was to start with 0 and increase by one for each value?

Well, this is death for a bit-flags enum that has more than 3 values! First of all, your first value is always zero by default. What if we had:. Thus, with bitwise enums you should either start at 1, or define a None value with value zero. Another problem is the default incrementation of the values.

Remember that Dither will default to 3. Well, what is 3 in binary? Notice that the binary 3 is the combination of the binary 1 and binary 2 1s and 2s bits both set. This means that if we used this enumeration as is, ShapeOptions.

Dither would be equal to ShapeOptions. Shadow which is not what we want at all.

Enumeration Types (C# Programming Guide) | Microsoft Docs

This is why we must make sure our values are bit-unique. That is, the integer value of each enumerated value must be represented by a single, unique bit.

The easiest way to do this is by assigning powers of two you can use decimal or hexadecimal if you prefer. Also, if we wanted, we could have defined the values using hex:. As long as we stick with powers of two we are guaranteed each value will be a single, unique bit. This does create a little more work on our end to guarantee that the bits are unique when we add new values.

Now when we look at Buffered in binary, it is no longer the combination of Durable ORd with Persistent:. In fact, if we combine them all, we can see that all the bits fit together nicely and are unique:. So how do we get values back out of a bit-flags enum? There are other ways to do this, of course, but this is the most typical. This is because remember that in a bitwise AND, the resulting bits are only set if both source bits are binary 1s.

Which is what we expect. It so happens that. We could re-write our flag checker to the following:. You probably noticed the [Flags] attribute applied to our enum when we started talking about bit-flags. This allows for some methods to work better with bit-flags in our enum s. In fact, the only thing the [Flags] attribute does is to allow the Enum. ToString method to print multiple flags for a bit-flag value. So the only functionality that adding [Flags] does is give you a better ToString.

The [Flags] attribute does give you a better ToString after all and it gives you a good semantic aid that shows the intention of the enum which makes your code better documented and easier to maintain. Print posted on Thursday, July 22, 5: My Links Home Contact Login. Combining Enum Values with Bit-Flags. WriteLine "Persistent" ; WriteLine "Durable" ; C , Software , Fundamentals. Combining Enum Values with Bit-Flags Hmm, Enum.

Combining Enum Values with Bit-Flags Sparkie: Enum so you can call it off of any enumeration instance: Combining Enum Values with Bit-Flags One thing to watch out for when using the HasFlag method is that you shouldn't check for your None 0 value using it, it will always return true. This may already be obvious to you, since you didn't include a check for it in your TestBits method.

Buffered; despite what you might think, the way HasFlag is implemented, whatever.

c++11 - Using scoped enums for bit flags in C++ - Software Engineering Stack Exchange

None will return true. Combining Enum Values with Bit-Flags Jordan: Combining Enum Values with Bit-Flags Thanks for the great post.

Using enum classes as type-safe bitmasks

Sometimes when you don't touch some of these basic things for a long time, a little refresher is great. Combining Enum Values with Bit-Flags Jeff: Atrophy can be a nasty thing Combining Enum Values with Bit-Flags This is one of the best answer so far, I have read online. Thanks for sharing with us. I had found another nice post with wonderful explanation on Enumeration in c , which is also helped me to complete my task. For more details of that post check out this link Combining Enum Values with Bit-Flags Just what I needed to point one of my new developers towards.

It's surprising how little this stuff is covered in your typical programming course these days. Combining Enum Values with Bit-Flags Also look http: Combining Enum Values with Bit-Flags The [Flags] line of code is completely worthless. Combining Enum Values with Bit-Flags [Flags] isnt worthless.

Combining Enum Values with Bit-Flags Great article!

Buffered While this may not matter in this case, it certainly does in the general case, where the mask could be multiple bits: Post A Comment Title: Please enter a title Name: Please enter your name Email: Email is not required, but it must be valid if specified. Please enter a comment Verification: My Links Home Contact Login News Welcome to my blog!

Enumerated type - Wikipedia

Combining Enum Values with Bit-Flags Two posts ago, I talked about the C enum and some of its pitfalls here.

For example, what if we created an enum like this: Combining Bitwise Values Remember that the binary OR operator will combine the bits of both numbers such that the resulting bit is 1 if either source bit is 1.

So armed with this knowledge, we can try to combine elements in our enum: So now, if we combine them using OR like above, we get: Persistent; Then we would have gotten: Default Enumeration Values Are Not Appropriate for Bitwise Enums Remember I said that the default numbering of enum values was to start with 0 and increase by one for each value?

What if we had: Also, if we wanted, we could have defined the values using hex: Now when we look at Buffered in binary, it is no longer the combination of Durable ORd with Persistent: Persistent; If we compare for Durable, we get: Likewise if we compare for Buffered, we should get zero: So if we run result through this function, we will get: Durable Which is what we expect.

We could re-write our flag checker to the following: The Curious FlagsAttribute You probably noticed the [Flags] attribute applied to our enum when we started talking about bit-flags. Only works with [Flags]: Allow ToString to print comma-separated list of enum flag values.

c enum bitwise operations

Works regardless of whether [Flags] is used or not: Parse will handle comma-separated list of values and combine them. Bit-wise math works will not even warn you if no [Flags] — though Resharper does. What [Flags] does not do: NET , C , CSharp , C Fundamentals , enum Big Flags Enumerations.

c enum bitwise operations

Please enter a title. Please enter your name. Please enter a comment.

inserted by FC2 system