Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
Bit-Field manipulation library

qBitField is a library of functions for creating and manipulating bit fields also known as bit arrays. These are series of zeroes and ones spread across an array of storage units, such as unsigned long integers.

Structure

Bit arrays are stored in data structures of type qBitField_t. This structure has two elements: an array of unsigned long integers called field for storing the bits and an integer size for storing the number of bits in the array.

Functions

Instance setup

Single-bit

Multiple-bits

Dump

  • qBitField_Dump() Copies n bytes from the bit-field instance to a designed memory area.

Working with bitfields

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "qbitfield.h"
int main( int argc, char *argv[] )
{
qBitField_t vPort; // create the bitfield instance
uint8_t vPortArea[ QBITFIELD_SIZE(48) ] = { 0 }; // Create the bitfield storage area to hold 48bits
uint16_t rWord;
qBitField_Setup( &vPort, vPortArea, sizeof(vPortArea) );
//we are going to write the following value in the bitfield = 0x8177AA55FF88
qBitField_WriteUINTn( &vPort, 0, 0xAA55FF88, 32 ); // write the first 32 bits
qBitField_WriteUINTn( &vPort, 32, 0x77, 8 ); // write one of the last two bytes
qBitField_WriteBit( &vPort, 47, 1 ); // write the last bit of the last byte
qBitField_WriteBit( &vPort, 40, 1 ); // write the first bit of the last byte
rWord = (uint16_t)qBitField_ReadUINTn( &vPort, 20, 16 ); // read a word at offset 24
printf("%02X %02X %02X %02X %02X %02X\r\n", vPortArea[0], vPortArea[1], vPortArea[2], vPortArea[3], vPortArea[4], vPortArea[5]);
return EXIT_SUCCESS;
}
#define QBITFIELD_SIZE(NBITS)
Use to determine the uint8_t array-size for a BitField.
Definition qbitfield.h:41
int qBitField_WriteUINTn(qBitField_t *const b, const size_t index, uint32_t value, size_t xBits)
Writes an unsigned n-bit value from the BitField.
Definition qbitfield.c:230
int qBitField_ClearAll(qBitField_t *const b)
Clear all the bits in the BitField.
Definition qbitfield.c:114
int qBitField_WriteBit(qBitField_t *const b, const size_t index, uint8_t value)
Writes one bit in a bitfield.
Definition qbitfield.c:189
int qBitField_Setup(qBitField_t *const b, void *const area, const size_t area_size)
Setup a initialize a BitField instance.
Definition qbitfield.c:95
uint32_t qBitField_ReadUINTn(const qBitField_t *const b, const size_t index, size_t xBits)
Reads an unsigned 32-bit value from the BitField.
Definition qbitfield.c:208
A BitField object.
Definition qbitfield.h:30