UDI_BFMASK, UDI_BFGET, UDI_BFSET


Bit-field helper macros

SYNOPSIS

	 #include <udi.h>
#define UDI_BFMASK(p, len) \

	(((1U<<(len))-1) << (p))
 
#define UDI_BFGET(val, p, len) \

	(((udi_ubit8_t)(val) >> (p)) & ((1U<<(len))-1))
 
#define UDI_BFSET(val, p, len, dst) \

	((dst) = ((dst) & ~UDI_BFMASK(p,len)) | \

		     (((udi_ubit8_t)(val) << (p)) & \

			    UDI_BFMASK(p,len)))
 

ARGUMENTS p is the bit position in the byte of the least significant bit in the bit field. The bit position p is 0 for the least significant bit in the byte, 7 for the most significant bit. 0 £ p £ 7.

len is the size in bits of the bit field. 1 £ len £ 8-p.

val is a udi_ubit8_t variable or value.

dst is a udi_ubit8_t variable into which a value will be deposited.

DESCRIPTION Bit-fields in these macros refer to sub-divisions of a byte and are defined by a 2-tuple (p,len) where p is the bit position in the byte of the least significant bit in the bit field, and len is the size in bits of the bit field. The bit position p is 0 for the least significant bit in the byte, 7 for the most significant bit. Note that 0 £ p £ 7 and 1 £ len £ 8-p.

UDI_BFMASK creates a p,len bit mask containing all 1's in the corresponding bit field, zeroes elsewhere.

UDI_BFGET extracts an unsigned p,len bit-field from val.

UDI_BFSET deposits val into the p,len bit-field in dst.

These macros must be called as if they, respectively, had the following functional interfaces:

udi_ubit8_t UDI_BFMASK (

		udi_ubit8_t p,

		udi_ubit8_t len);
 
udi_ubit8_t UDI_BFGET (

		udi_ubit8_t val,

		udi_ubit8_t p,

		udi_ubit8_t len);
 
void UDI_BFSET (

		udi_ubit8_t val,

		udi_ubit8_t p,

		udi_ubit8_t len,

		udi_ubit8_t dst);
 

Note that UDI_BFSET modifies dst, and dst must be an lvalue (assignable on the left side of an assignment statement).

22.2.2.2 Multi-Byte Macros

The multi-byte helper macros, UDI_MBGET and UDI_MBSET and their variants, are used to extract and deposit multi-byte quantities from a structure which has been constructed according to the byte-by-byte layout rules given in Section 22.2.1.1.

These macros have arguments called "structp" and "field". The structp argument is a pointer to a structure that contains N single-byte (and byte-aligned) members whose names are "field"0, "field"1, ... "field"N, which together represent a multi-byte quantity in the structure. "field"0 is the least significant byte; "field"N is the most significant.

For example, a structure that has a 3-byte "sum" field might have field names of sum0, sum1, and sum2, and the complete 24-bit field could be extracted from the structure into a variable, my_sum, using the UDI_MBGET macro as follows:

    my_sum = UDI_MBGET(3, &my_struct, sum);

To write a value into this 3-byte field, use the UDI_MBSET macro as follows:

    UDI_MBSET(3, &my_struct, sum, my_sum);


UDI Core Specification Contents