My Project
Functions
field.h File Reference

Defines interface of field.c functions (do not modify) More...

#include <stdbool.h>

Go to the source code of this file.

Functions

int getBit (int value, int position)
 
int setBit (int value, int position)
 
int clearBit (int value, int position)
 
int getField (int value, int hi, int lo, bool isSigned)
 
int setField (int oldValue, int hi, int lo, int newValue)
 
int fieldFits (int value, int width, bool isSigned)
 

Detailed Description

This file defines the interface to a C file field.c that you will complete. You will learn how to use the C language operators for binary and (&), binary or (|), and binary not (~). You will also use the C language bit shift operators (<< and >>).

Binary and (&) will be used to extract the value of a bit and to set a bit to 0. This relies on the fact that the binary and (&) of a value and 1 results in the original value. Binary and (&) of a value and 0 results in 0. Binary or (|) is use to set a bit to 1. This relies on the the fact that binary or (|) of 1 and anything results in a 1.

You will create masks. A mask is a bit pattern that contains 0's and 1's in appropriate places so when the binary and/or operation is performed, the result has extracted/modified the bits of interest. In the following examples B stands for bits of interest while x stands for a bit that is not of interest. Note that, in general, the bits of interest need not be consecutive. In this code, we will be dealing with consecutive sets of bits.


  value:    xxxBBBBxxxxx  value: xxxBBBBxxxxx   value: xxxBBBBxxxxx
  mask:     000111100000  mask:  111000011111   mask:  000111100000
  -------   ------------         ------------          ------------
  and(&)    000BBBB0000   and(&) xxx0000xxxxx   or(|)  xxx1111xxxxx
  result:   isolate field        clear field           set field

You may create masks on the fly using C code, or you may pre compute masks and store them in an array and just use the one you need. Note the mask for clearing bits is the binary not (~) of the mask for extracting bits.

Bit positions are numbered from 31 to 0 with 0 being the least significant bit. The bit position corresponds to the power of 2 in the binary representation.

As an example of how the fields interface is useful consider the IEEE half precision floating point representation. In the IEEE half precision floating point representation, 16 bits are used to represent a floating point number. This is shown in the following table where S represents the sign, E represents bits for the exponent, and F represents bits for the fraction.

bit position31..1615141312111098 76543210
meaningnot usedSEEEEEFF FFFFFFFF

To extract fields from the value, you would make calls like:


   int sign = getField(value, 15, 15, false);
   int exp  = getField(value, 14, 10, false);
   int frac = getField(value,  9,  0, false);

In the following methods, hi and lo are NOT guaranteed to be ordered! So getField(value, 5, 10, false) and getField(value, 10, 5, false) must both produce identical results.

Author
Fritz Sieker

Function Documentation

int clearBit ( int  value,
int  position 
)

Set the specified bit in a value to 0.

Parameters
valuethe source value or bit pattern
positionthe bit position to be set (0..31)
Returns
An integer value that is the original value with the bit set to 0.
int fieldFits ( int  value,
int  width,
bool  isSigned 
)

Determine if a value will fit in a specified field

Parameters
valuethe source value
widththe number of bits holding the value
isSignedfalse means the field is unsigned, true means the field is signed
Returns
zero if the field does NOT fit. Return 1 if the value fits.
int getBit ( int  value,
int  position 
)

Get the specified bit from a value.

Parameters
valuethe source value or bit pattern
positionthe bit position to get (0..31)
Returns
1 when the bit is set, and 0 otherwise.
int getField ( int  value,
int  hi,
int  lo,
bool  isSigned 
)

Extract the field (possibly signed) between bits hi and lo (inclusive).

Parameters
valuethe source value or bit pattern
hithe bit position of one end of the field
lothe bit position of the other end of the field
isSignedfalse means the field is unsigned, true means the field is signed
Returns
The value of the field. Sanity check example: if the field is three bits wide and unsigned, the result will be a value between 0 and 7, regardless of the actual position of the bits in value. If the value is signed, the result will be between -4 and 3.
int setBit ( int  value,
int  position 
)

Set the specified bit in a value to 1.

Parameters
valuethe source value or bit pattern
positionthe bit position to be set (0..31)
Returns
An integer value that is the original value with the bit set to 1.
int setField ( int  oldValue,
int  hi,
int  lo,
int  newValue 
)

Change the bits of oldValue between hi and lo to the newValue, leaving the other bits unchanged.

Parameters
oldValuethe original value
hithe bit position of one end of the field
lothe bit position of the other end of the field
newValuethe new value to put in the field (use lower bits)
Returns
the value after replacing only the hi to low bits inclusive by newValue