Defining Programming Standards   
for Professional Programmers 
  

         

Home

Contents

1: Standards

2: Psychological Factors

3: General Principles

4: Commenting

5: Naming

6: Code Layout

7: File Layout

8: Language Usage

9: Data Usage

10: Programming Usage

11: Implementing Standards

A: Example Standard

B: References

C: Glossary

Syque

About

Share this page:

Google
C Style
syque.com
Web

 

 

Books and
more at:

USA:

In association with amazon.com

UK:

In Association with Amazon.co.uk

Canada:

In Association with amazon.ca

 

 

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 -->

 

 

  © Syque 1995-2006

Massive Content -- Maximum Speed