Inline Bit Test / Complement / Reset / Set

Header

	bitops.h

Prototype

	int _inline_bt(unsigned *p, unsigned index);
	int _inline_btc(unsigned *p, unsigned index);
	int _inline_btr(unsigned *p, unsigned index);
	int _inline_bts(unsigned *p, unsigned index);

Description

p is a non-NULL pointer to an array of unsigneds. index is a bit number, starting with bit 0 of p[0], and progressing. It addresses bits like the expression:
	p[index / (sizeof(unsigned)*8)] & (1 << (index & ((sizeof(unsigned)*8) - 1)))
_inline_bt tests the bit.

_inline_btc tests and complements the bit.

_inline_btr tests and resets (sets to 0) the bit.

_inline_bts tests and sets the bit.

These functions are inlined by the compiler, bringing to bear the full power of the code generator on them. This can result in some surprising speedups when they are used as part of a bit array solution.

Return Value
All return a non-zero value if the bit was set, and a zero if it was clear.

Compatibility
DOS, Windows 3.x, Phar Lap, DOSX, Win32

See Also
_inline_bsf, _inline_bsr

Example


	#include <stdio.h>
	#include <bitops.h> 

	int main()
	{   
	    unsigned array[2];

	    array[0] = 2;
	    array[1] = 0x100;

	    printf("btc(array, 35) = %d\n", _inline_btc(array, 35));
	    printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);

	    printf("btc(array, 35) = %d\n", _inline_btc(array, 35));
	    printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);

	    printf("bts(array, 35) = %d\n", _inline_bts(array, 35));
	    printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);

	    printf("btr(array, 35) = %d\n", _inline_btr(array, 35));
	    printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);

	    printf("bt(array, 1) = %d\n", _inline_bt(array, 1));
	    printf("array = [0]:x%x, [1]:x%x\n", array[0], array[1]);

	    return 0;
	} 

Output
	btc(array, 35) = 0
	array = [0]:x2, [1]:x108
	btc(array, 35) = -1
	array = [0]:x2, [1]:x100
	bts(array, 35) = 0
	array = [0]:x2, [1]:x108
	btr(array, 35) = -1
	array = [0]:x2, [1]:x100
	bt(array, 1) = -1
	array = [0]:x2, [1]:x100
Bugs
Compiler versions prior to 0x803 would sometimes produce incorrect answers if index was a constant.