473,761 Members | 9,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bitfield size check

Sometimes you want to use a bitfield to hold an enum value. In such
cases you would only use as many bits as are needed to encode the
full set of enum values. And it is a pain to recompute and updated
the bitfield length each time you add new enum values. But there is
a way to make the compiler do it for you. Like this:

enum Enums
{
Enum1,
Enum2,
Enum3,
Enum4,

TotalEnums,
};

struct A
{
Enums enumValue:COMPU TE_BITFIELD_LEN GTH(TotalEnums) ;
};

And COMPUTE_BITFIEL D_LENGTH would look something like this:

#define COMPUTE_BITFIEL D_LENGTH(n) ((n) < 1) ? 0 : ((n) < 3) ? 1 :\
((n) < 5) ? 2 : ((n) < 9) ? 3 : ((n) < 17) ? 4 : ((n) < 33) ? 5 :\
((n) < 65) ? 6 : ((n) < 129) ? 7 : ((n) < 257) ? 8 : 32
It works fine.
Unless the number Enums is exactly the power of 2, in wich case
some compilers give warning akin to this:
"A::enumVal ue is too small to hold all values of Enums" appears.
It happens because the compiler considers TotalEnums to be part of the
enum set in wich case the total number of values become power of 2 plus
one and you need one additional bit.

Is there a way to compute the size of the enum structure
without introducing a new enum value?

Thanks,
Andy.
Nov 14 '05 #1
3 2935
Andy Venikov wrote:
Sometimes you want to use a bitfield to hold an enum value. In such
cases you would only use as many bits as are needed to encode the
full set of enum values. And it is a pain to recompute and updated
the bitfield length each time you add new enum values. But there is
a way to make the compiler do it for you. Like this:

enum Enums
{
Enum1,
Enum2,
Enum3,
Enum4,

TotalEnums,
The trailing comma is allowed in C99, but forbidden
in C89.
};

struct A
{
Enums enumValue:COMPU TE_BITFIELD_LEN GTH(TotalEnums) ;
I think you mean `enum Enums enumValue ...'. However,
I think you should instead use `unsigned int enumValue ...',
for two reasons: First, only the `int' types (and `_Bool'
in C99) are portable "base types" for bit-fields -- the
compiler is allowed to accept other types, but is not
required to do so. Second, your compiler's willingness to
accept an enum type as a bit-field base is actually making
trouble for you -- ditch the non-portable type, and the
trouble will probably go away.
};

And COMPUTE_BITFIEL D_LENGTH would look something like this:

#define COMPUTE_BITFIEL D_LENGTH(n) ((n) < 1) ? 0 : ((n) < 3) ? 1 :\
((n) < 5) ? 2 : ((n) < 9) ? 3 : ((n) < 17) ? 4 : ((n) < 33) ? 5 :\
((n) < 65) ? 6 : ((n) < 129) ? 7 : ((n) < 257) ? 8 : 32
Note that since the `int' flavors are the widest portable
bit-field base types, portable bit-fields can be no wider
than `int'. `int' can be as narrow as sixteen bits, so a
bit-field width of thirty-two is not portable.
It works fine.
Unless the number Enums is exactly the power of 2, in wich case
some compilers give warning akin to this:
"A::enumVal ue is too small to hold all values of Enums" appears.
It happens because the compiler considers TotalEnums to be part of the
enum set in wich case the total number of values become power of 2 plus
one and you need one additional bit.

Is there a way to compute the size of the enum structure
without introducing a new enum value?


You could instead keep track of the highest actual value:

enum Enums { A, B, C, D };
#define TotalEnums (D + 1)

Of course, less "regular" enum types could make trouble:

enum Ugly { W = -42, X = 1, Y, Z };
#define TotalUglies (Z + 1)

.... might fool you into thinking you need only two bits to
store an `enum Ugly' value.

--
Er*********@sun .com

Nov 14 '05 #2
sw**********@ne tscape.net (Andy Venikov) writes:
Sometimes you want to use a bitfield to hold an enum value. In such
cases you would only use as many bits as are needed to encode the
full set of enum values. And it is a pain to recompute and updated
the bitfield length each time you add new enum values. But there is
a way to make the compiler do it for you. Like this:

enum Enums
{
Enum1,
Enum2,
Enum3,
Enum4,

TotalEnums,
};

struct A
{
Enums enumValue:COMPU TE_BITFIELD_LEN GTH(TotalEnums) ;
};

And COMPUTE_BITFIEL D_LENGTH would look something like this: [snip] It works fine.
Unless the number Enums is exactly the power of 2, in wich case
some compilers give warning akin to this:
"A::enumVal ue is too small to hold all values of Enums" appears.
It happens because the compiler considers TotalEnums to be part of the
enum set in wich case the total number of values become power of 2 plus
one and you need one additional bit.

Is there a way to compute the size of the enum structure
without introducing a new enum value?


Perhaps something like this:

enum Enums {
Enum1,
Enum2,
Enum3,
Enum4,
LastEnum = Enum4
};

Adjust your big ugly macro accordingly, and don't forget to update the
value assigned to LastEnums if you add new values at the end.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
kal
Keith Thompson <ks***@mib.or g> wrote in message news:<ln******* *****@nuthaus.m ib.org>...
Perhaps something like this:

enum Enums {
Enum1,
Enum2,
Enum3,
Enum4,
LastEnum = Enum4
};


I like this one. But one might as well specify the number
of bits as an enum constant if one insists on having this
pseudo #define as part of the enum definition.

enum ugly {low=-1, medium=0, high=1, BITS=3};

--

"See ma, no macro!"
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4013
by: User | last post by:
Anyone have ideas which os command could be used to get the size of a file without actually opening it? My intention is to write a script that identifies duplicate files with different names. I have no trouble getting the names of all the files in the directory using the os.listdir() command, but that doesn't return the file size. In order to be identical, files must be the same size, so I want to use file size as the first criteria,...
9
2123
by: Sashafay | last post by:
Hi, I have experience of strange behavior of my Access 97 database. I went to the existing database, which has a size of 30mb, and change three lines of VBA codes behind reports. 15 reports have been modified. And after compacting database I got 33.5mb database. What did I change that caused database jump on 3.5 megabytes of size? What can I do to clean up that absolutely odd behavior of Access 97? Thanks in advance,
9
5254
by: Davide Bruzzone | last post by:
Greetings all... I need to create a number of bitfield structs whose contents are smaller than the size of an int. For example: typedef struct foo FOO; struct foo { unsigned char fieldOne: 2, fieldTwo: 6; };
4
10471
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
35
2686
by: munish.nr | last post by:
Hi All, I want to know the size of file (txt,img or any other file). i knoe only file name. how i can acheive this. does anybody is having idea about that. plz help. rgrds, Munish Nayyar
4
8967
by: John Smith | last post by:
Hi I'm porting some C++ code to new platforms and have some 1-byte aligned structures which need a specific size. Since datatypes can vary on different platforms (which I found out the hard way since longs are not the same size on win64 and linux x64) I would like to do a check at compile time to make sure things are correct. This will ease future work. Is it possible to do something like the following at preprocessing stage:
14
3429
by: Galen Somerville | last post by:
My current screen resolution is set to 1024 x 768. My form size always comes up as 1032 x 748. I have tried the help sample ' Retrieve the working rectangle from the Screen class ' using the PrimaryScreen and the WorkingArea properties. Dim workingRectangle As System.Drawing.Rectangle = _ Screen.PrimaryScreen.WorkingArea ' Set the size of the form slightly less than size of
2
1553
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within dot net is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating properly (I have a file that has a size of 1 KB but has 14 bytes in it so the conversion isn't working right). Is there some method/property out there that will get the actual size of the file? Also, would there be a method like this that will...
6
3851
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of the bit-field object from the size used to specify the field. Could this be considered a defacto standard now (at least for 8 bit sized bit-fields)? Any recent compilers not allowing this?
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.