Bit Fields


Many of the user-defined data types we have examined so far are available, in one form or another, in other programming languages such as Java and Visual Basic. However, C++ offers one intriguing user-defined type that is not present in most languages. That type is the bit field. Normally, in any programming language, any variable is of some particular data type (int, float, bool, etc.) and takes up a certain number of bytes (remember that there are eight bits in a byte). However, C++ allows you to access individual bits. This is of particular importance when writing software that communicates with hardware, and in the telecommunications industry.

When you create a bit field, you create it like you would any normal structure. However, each individual element of the structure is declared as an unsigned, then given a name and a colon. After the colon is a number representing how many bits this particular element occupies.

It is beyond the scope of this book to explore either hardware programming or telecommunications programming. However, both of these areas of software development make extensive use of the C++ programming language, and of features such as bit fields. The following example should help illustrate the basics of using a bit field. Although the example is about checking the status of a serial line and taking appropriate action, the specific code for the particular actions to take is beyond the scope of this book and, therefore, the functions called are empty, with just comments in them. The studious reader can easily find appropriate serial line code examples on the Internet and expand this example. Our interest now is simply to study the bit field itself, not its potential applications.

Example 8.7

Step 1: Enter the following code into your favorite text editor.

#include <iostream> using namespace std;   // declare a bit field struct status {      unsigned changeinline: 1;      unsigned cleartosend:1;      unsigned inactive:1;      unsigned ringing:1;      unsigned signalreceived:1; }; // function prototypes void dialnumber(); void answerphone(); void senddata(); int main()    {      // delcare an instance      // of the bit-field status linestatus;      if (linestatus.cleartosend)      senddata();      if (linestatus.inactive)    dialnumber();            if(linestatus.ringing)    answerphone();      return 0;    } void dialnumber() { // place code here to dial } void answerphone() {  // place code here to answer the phone } void senddata() {  // place code here to send the data }

Step 2: Compile that code.

The important thing to realize is that the bit field allows you to directly speak the same language as PC hardware, and telecommunications lines. These devices do not send data in types such as int, long, or double. They send a stream of individual bits. C++ allows you to directly work with those individual bits in a way that few other languages do.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net