views:

110

answers:

1
+1  Q: 

4-bit Enum in C#

I know that its possible to make enums that use signed or unsigned 64, 32, 16, and 8 bit values as their underlying valud type using (:ulong, :uint, :ushort, :byte). But is it possible to create a 4 bit enum?

(I'm writing some code that will interop with C++ and the struct that I have in C# for a return type has one field that would be most natural as a 4-bit struct.)

+2  A: 

From the C# spec:

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

None of these are 4-bit types. You'd have the same problem on the C++ side as well.

I. J. Kennedy
C++ has bit fields. You can say how many bits a struct field uses.
Gabe
Yeah, on the C++ side, its just a matter of doing ":4" in the struct and then they #define the 16 possible values. It's easy here on the C# side just to merge two of these :4 values into one byte. But then I have to look at the lower 4 bits of it and the upper 4 bits separately.
Michael Covelli