Вы находитесь на странице: 1из 2

/*----------------------------------------------------------------------------*\

| |
| Author : Endless Loop |
| |
\*----------------------------------------------------------------------------*/

/* This header defines macros to operate on unsigned integers through operations


** oriented to bits, masks, and fields.
*/

#ifndef BITLIB_H
#define BITLIB_H

/*
** ON OPERATIONS
*/
/* Sets to 1 the bit of 'target' in position 'bitpos' */
#define bit_on(target, bitpos) ( (target) |= (1U<<(bitpos)) )
/* Sets to 1 the bits of 'target' which position is indicated by 'mask'.
** A position N is indicated by 'mask' if the N-bit of 'mask' is set to 1.
*/
#define mask_on(target, mask) ( (target) |= (mask) )
/* Sets to 1 the bits of 'target' which position is included in the interval
** ['fstart'; 'fend'].
*/
#define field_on(target, fstart, fend) \
( (target) |= (2U<<(fend)) - (1U<<(fstart)) )
/*
** OFF OPERATIONS
*/
/* Sets to 0 the bit of 'target' in position 'bitpos' */
#define bit_off(target, bitpos) ( (target) &= ~(1U<<(bitpos)) )
/* Sets to 0 the bits of 'target' which position is indicated by 'mask'.
** A position N is indicated by 'mask' if the N-bit of 'mask' is set to 1.
*/
#define mask_off(target, mask) ( (target) &= ~(mask) )
/* Sets to 0 the bits of 'target' which position is included in the interval
** ['fstart'; 'fend'].
*/
#define field_off(target, fstart, fend) \
( (target) &= ~(2U<<(fend)) + (1U<<(fstart)) )
/*
** SET OPERATIONS
*/
/* Sets the bit of 'target' in position 'bitpos' to 0 if 'source' has value 0,
** otherwise sets it to 1.
*/
#define bit_set(target, bitpos, source) \
( (source) ? bit_on(target, bitpos) : bit_off(target, bitpos) )
/* Sets the bits of 'target' which position is indicated by 'mask' to the value
** of the bits of 'source' in the same position.
** A position N is indicated by 'mask' if the N-bit of 'mask' is set to 1.
*/
#define mask_set(target, mask, source) \
( (target) ^= ((target)^(source)) & (mask) )
/* Sets the bits of 'target' which position is included in the interval
** ['fstart'; 'fend'] to the value if the bits of 'source' included in
** the interval [0; 'fend' - 'fstart']. Relative order is preserved.
*/
#define field_set(target, fstart, fend, source) \
( (target) ^= ( (target) ^ ((source)<<(fstart)) ) \
& ( (2U<<(fend)) - (1U<<(fstart)) ) )
/*
** GET OPERATIONS
*/
/* Returns the value of the bit of 'target' in position 'bitpos' */
#define bit_get(target, bitpos) \
( ( (target) & (1U<<(bitpos)) ) != 0 )
/* Returns the value of the field of 'target' bounded by the interval
** [fstart; fend].
*/
#define field_get(target, fstart, fend) \
( ( (target) & ((2U<<(fend))-1) ) >> (fstart) )

#endif /* BITLIB_H */

Вам также может понравиться