OT: Bitmasks vs. Bit-Fields



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 31/10/2004 at 09:56, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   8.503 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
    I've never used bit-fields in either C or C++. My mainstay is bitmasks (flag |= and flag &= ~). But, my current plugin is an occassion for optimizing the buh-jeezus out of memory usage since it deals with large arrays (or lists) of classes and creates large permanent arrays in the document. So each class should be as optimal as possible for both speed and memory usage, but more of the latter. There are many instances where groups of small (UCHAR) member variables could easily be concatenated into a single member variable to conserve memory, but dealing with multi-bit fields in the bitmask format has shown to be "not-always-dependable" or flaky (e.g.: I've noticed that for VC++6, flag &= ~value tends to fail to set the bit to 0. This has occurred on several occassions and befuddles me since I have enough experience with bitmasks (over 17 years) to know what works and doesn't. I suspect the typical lousy MS support of standard language features).

    So far, both pros and cons have been encountered concerning use of bit-fields. I just wanted to know what you other C++ programmers think about using them - are they worth the overhead which causes some speed loss?

    Thanks,
    Robert



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 31/10/2004 at 12:54, xxxxxxxx wrote:

    Although there is a slight speed hit, I find them to be very usable. Less cumbersome to use than bitmasks (more like variables) and setting values (even 0) works flawlessly. Of course, the only downside are the caveats - no pointers, no references - but I use them class-internally and use Get/Set methods for external retrieval of particular bits.

    I also realize that there might need to be consideration of platform dependency in using them. Will see when the plugin is being ported over to MacOS.

    Robert


Log in to reply