def bit(x):
return (1 << x)
def get_bit(num, bit):
return (num>>bit)&0x01
for x in xrange(0, 64):
print("0x%x" %(1 << x))
for y in xrange(0,10):
print get_bit(10, y)
#include <stdio.h>
#define BITMASK(bit) (1 << (bit))
// 获取某bit位数据
#define GETBIT(val,bit) (((val) & BITMASK(bit)) > 0)
// 设置某bit位数据为1
#define SETBIT(val,bit) (val | BITMASK(bit))
// 设置某bit位数据为0
#define RESETBIT(val,bit) (val & ~BITMASK(bit))
void main()
{
int num = 10;
unsigned char bit = GETBIT(num, 1);
printf("%d\n", bit);
int num2 = RESETBIT(num, 1);
printf("%d\n", num2);
int num3 = SETBIT(num, 1);
printf("%d\n", num3);
// 240
num = RESETBIT(num, 0);
num = RESETBIT(num, 1);
num = RESETBIT(num, 2);
num = RESETBIT(num, 3);
num = SETBIT(num, 4);
num = SETBIT(num, 5);
num = SETBIT(num, 6);
num = SETBIT(num, 7);
printf("%d\n", num);
}
发表评论