首页 > 代码库 > _BitScanForward, _BitScanForward64

_BitScanForward, _BitScanForward64

_BitScanForward(64)的使用得include头文件intrin.h
unsigned char _BitScanForward( unsigned long * Index, unsigned long Mask);unsigned char_BitScanForward64( unsigned long * Index, unsigned __int64 Mask);

_BitScanForward(64)函数的作用是以2进制的形式从Mask的最低有效位(least significant bit)向最高有效位(most significant bit )搜索,直到找到1.

eg.  

  unsigned long index0, index1, index2;

  unsigned long mask0 =   1B;

  unsinged long mask1 =   100B;

  unsigned long mask2 = 1100B;

  _BitScanForward(&index0, mask0);

  _BitScanForward(&index1, mask1);

  _BitScanForward(&index2, mask2);

得到的结果是:

  index0 = 0; //因为1出现在第一位

  index1=index2 = 2;//100B和1100B的第一个1都是出现在第三位

note:在编程中计数都是从0开始,所以出现在第3位的1得到的结果是2.

 1 #include <iostream> 2 #include <intrin.h> 3 using namespace std; 4  5 #pragma intrinsic(_BitScanForward) 6  7 int main() 8 { 9     unsigned long mask = 0x1000;10     unsigned long index;11     unsigned char isNonzero;12 13     cout << "Enter a positive integer as the mask: " << flush;14     cin >> mask;15     isNonzero = _BitScanForward(&index, mask);16     if (isNonzero)17     {18         cout << "Mask: " << mask << " Index: " << index << endl;19     }20     else21     {22         cout << "No set bits found.  Mask is zero." << endl;23     }24     return 0;25 }

 

_BitScanForward, _BitScanForward64