Browse Prior Art Database

Portable Bit Manipulation Techniques in 'C'

IP.com Disclosure Number: IPCOM000102636D
Original Publication Date: 1990-Dec-01
Included in the Prior Art Database: 2005-Mar-17
Document File: 4 page(s) / 111K

Publishing Venue

IBM

Related People

Busboom, CD: AUTHOR [+3]

Abstract

Disclosed is a method of handling bits within a word or byte consistently across multiple target processors in the 'C' programming language. Applications having externally-imposed data formats, such as interfaces to hardware devices or software architected data, which require a definite format, and location of bits within word and byte structures can be easily ported to different target machines.

This text was extracted from an ASCII text file.
This is the abbreviated version, containing approximately 52% of the total text.

Portable Bit Manipulation Techniques in 'C'

       Disclosed is a method of handling bits within a word or
byte consistently across multiple target processors in the 'C'
programming language.  Applications having externally-imposed data
formats, such as interfaces to hardware devices or software
architected data, which require a definite format, and location of
bits within word and byte structures can be easily ported to
different target machines.

      The ANSI standard for 'C' provides a construct for bit fields
within a structure, but leaves the details as
implementation-dependent.  This means that the following problems may
be encountered when attempting to support identical function on
multiple target processors using the ANSI 'C' bit fields construct.
-  Bit field data may overlap a word boundary on one implementation,
but not another.
-  Location of bit fields may be assigned from right to left on one
implementation, and from left to right on another.
-  Storage of the bit field data spanning multiple bytes may be
contiguous on one implementation, but word aligned on another.
-  Storage of the bit field data within a word may be byte swapped in
one implementation, and in order in another.

      The problems resulting from the implementation-defined bit
fields in 'C' can be overcome by using the method described herein.

      An example demonstrating the problem and the solution follows.
NOTE:  This example assumes that the word size of the target machines
is two bytes.

      Suppose we have a 16-bit software-architected data structure
(see Fig. 1) called Flagword.  For the definition of Flagword we have
the following 'C' bit field structure:
       struct {
          unsigned int a : 1;
          unsigned int b : 1;
          unsigned int c : 1;
          unsigned int d : 1;
          unsigned int e : 1;
          unsigned int f : 1;
          unsigned int g : 1;
          unsigned int h : 1;
          unsigned int i : 1;
          unsigned int j : 1;
          unsigned int k : 1;
          unsigned int l : 1;
          unsigned int m : 1;
          unsigned int n : 1;
          unsigned int o : 1;
          unsigned int p : 1;
       } Flagword;

      Assume that 'C' compiler X and 'C' compiler Y are used for
target processor X and Y, respectively, and differ in implementation
of the 'C' bit field construct in the following ways.
-  Compiler X assigns fields from left to right and stores bytes
within memory words from left to right.
-  Compiler Y assigns fields from right to left and stores bytes
within memory words from right to left.

      Compiler X would produce...