Tutorial by Examples

A simple bit-field can be used to describe things that may have a specific number of bits involved. struct encoderPosition { unsigned int encoderCounts : 23; unsigned int encoderTurns : 4; unsigned int _reserved : 5; }; In this example we consider an encoder with 23 bits of sin...
#include <stdio.h> int main(void) { /* define a small bit-field that can hold values from 0 .. 7 */ struct { unsigned int uint3: 3; } small; /* extract the right 3 bits from a value */ unsigned int value = 255 - 2; /* Binary 11111101 */ small.u...
Bit-fields give an ability to declare structure fields that are smaller than the character width. Bit-fields are implemented with byte-level or word-level mask. The following example results in a structure of 8 bytes. struct C { short s; /* 2 bytes */ char c; /* 1 ...
A bit-field is used to club together many variables into one object, similar to a structure. This allows for reduced memory usage and is especially useful in an embedded environment. e.g. consider the following variables having the ranges as given below. a --> range 0 - 3 b --> range 0 -...
Arrays of bit-fields, pointers to bit-fields and functions returning bit-fields are not allowed. The address operator (&) cannot be applied to bit-field members. The data type of a bit-field must be wide enough to contain the size of the field. The sizeof() operator cannot be applied to a b...

Page 1 of 1