digitalmars.com                      
Last update Sun Mar 4 12:00:57 2018

bitops.h

bitops.h provides an interface to the bit instructions on the 386 and higher Intel processors. The compiler recognizes these functions and directly generates inline code for them, which can be substantially faster than implementing them as separate functions. The functions are very useful as building blocks for bit vectors.

_inline_bsf

Header
bitops.h
Prototype
int _inline_bsf(unsigned v);
int _inline_bsr(unsigned v);
Description
_inline_bsf scans the bits in v starting with bit 0, looking for the first set bit.

_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;
} 
Output
bsf(x21) = 0
bsr(x21) = 5

_inline_bt

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
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.
Home | Compiler & Tools | IDDE Reference | STL | Search | Download | Forums