[Home]
[Search]
[Contents]
bitops.h
int _inline_bsf(unsigned v); int _inline_bsr(unsigned v);
_inline_bsr scans the bits in v from the most significant bit to the least significant bit, looking for the first set 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
Both return the bit number of the first set bit.
The return value is undefined if v is zero.
Compatibility
DOS, Windows 3.x, Phar Lap, DOSX, Win32
Example
#include <stdio.h>
#include <bitops.h>
int main()
{
unsigned v;
int x;
v = 0x21;
x = _inline_bsf(v);
printf("bsf(x%x) = %d\n", v, x);
x = _inline_bsr(v);
printf("bsr(x%x) = %d\n", v, x);
return 0;
}
Outputbsf(x21) = 0 bsr(x21) = 5
bitops.h
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);
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
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]:x100Bugs