syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 9 : Data Usage

PART 4 : USAGE

CHAPTER 9 : Data Usage
9.1 Declarations
9.2 Using floating point numbers
9.3 Using 'typedef'
9.4 Using global data
9.5 Using Structures
9.6 Using Unions
9.7 Using Arrays
9.8 Using Pointers
9.9 Using bit structures
9.10 Using Constants
9.11 Using 'static' declarations
9.12 Initializing variables
9.13 Summary

<--Prev page | Next page -->

 

9.9  Using bit structures

Bit structures are typically used where space is at a premium, or where hardware registers must be matched. Style-wise the bits used give a message about the maximum size of the contained number:

 

struct
{
    unsigned    Handle      :3;     /* range: 0 to 7  */
    unsigned    Width       :5;     /* range: 0 to 32 */
    unsigned    Height      :6;     /* range: 0 to 64 */
    unsigned    bOpen       :1;     /* range: 0 or 1  */
    unsigned    bChanged    :1;     /* range: 0 or 1  */
} Window;

---------------------------------------------------------------

Bit structures, however, are highly non-portable. There is also a danger of only a loose functional relationship between the structure elements - the common factor being the reduced bit usage. They may also have a significant performance cost.

9.9.1  Bit Structures vs. Bit operators

An alternative to bit structures is to directly use bit operations, where masks and boolean operations are used to access fields. This may be more efficient, but it is seldom as clear. The equivalent of accessing Window.Width in the above example is:

 

WindowWidth = (WindowBits & WIN_WIDTH_MASK) >> WIN_WIDTH_POSN;

 

Bit operations do allow operations to be combined. Bit structures, on the other hand are clearer in use, and may allow structures larger than words. Thus they are usually preferable. If, however a bit-layout is defined in a program interface, then using bit operations is a more portable method.

 

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |

 

You can buy books here

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-2015
Massive Content -- Maximum Speed