码迷,mamicode.com
首页 > 其他好文 > 详细

_BitScanForward, _BitScanForward64

时间:2014-12-01 12:39:46      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   使用   sp   for   

_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     else
21     {
22         cout << "No set bits found.  Mask is zero." << endl;
23     }
24     return 0;
25 }

 

_BitScanForward, _BitScanForward64

标签:style   blog   io   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/JeroZeng/p/4134703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!