bittest & bittestandset
// bittest.cpp
// processor: x86, IPF, x64
#include <iostream>
#include <stdio.h>
#include <intrin.h>//for _bittest() & _bittestandset()
typedef unsigned char byte;
//取32位整形数组的第i(i从0开始)位,buf为数组指针,(long *)buf+(i>>5)用来选择数组中的每一个元素,
//(i&31)用来对应相应数组元素的bit位(0-31)
#define getbit(buf,i) _bittest((long *)buf+(i>>5),(i&31))
//将32位整形数组的第i位置为1
#define setbit(buf,i) _bittestandset((long *)buf+(i>>5),(i&31))
#define MAX 2
using namespace std;
int main()
{
unsigned i;
unsigned nBit = MAX<<5;
unsigned barray[MAX]={7,15};
setbit(barray,3);//set the 4th bit of barray to be 1
for (i = 0; i < nBit; i++)
{
byte ch = getbit(barray, i);
printf("%d",ch);
if (!((i+1)%32))//display each element by line
{
printf("\n");
}
}
}
// processor: x86, IPF, x64
#include <iostream>
#include <stdio.h>
#include <intrin.h>//for _bittest() & _bittestandset()
typedef unsigned char byte;
//取32位整形数组的第i(i从0开始)位,buf为数组指针,(long *)buf+(i>>5)用来选择数组中的每一个元素,
//(i&31)用来对应相应数组元素的bit位(0-31)
#define getbit(buf,i) _bittest((long *)buf+(i>>5),(i&31))
//将32位整形数组的第i位置为1
#define setbit(buf,i) _bittestandset((long *)buf+(i>>5),(i&31))
#define MAX 2
using namespace std;
int main()
{
unsigned i;
unsigned nBit = MAX<<5;
unsigned barray[MAX]={7,15};
setbit(barray,3);//set the 4th bit of barray to be 1
for (i = 0; i < nBit; i++)
{
byte ch = getbit(barray, i);
printf("%d",ch);
if (!((i+1)%32))//display each element by line
{
printf("\n");
}
}
}