c语言常见的置0和置1操作,【c语言】实现对一个8bit数据(unsigned char 类型)的指定位(例如第n位)置0或者置1操作,并保持其他位不变...

// 实现对一个8bit数据(unsigned char 类型)的指定位(例如第n位)置0或者置1操作,并保持其他位不变

#include

void bit_set(unsigned char *p_data, unsigned char position, int flag)

{

unsigned c;

unsigned char a = 1;

a = a << (position - 1);

if (flag == 1)

{

*p_data = *p_data | a;

}

else

{

c = ~a;

*p_data = *p_data & c;

}

}

int main()

{

int pos;

int flag;

unsigned char P_data;

printf("please enter the number:");

scanf("%d", &P_data);

printf("please enter the position:");

scanf("%d", &pos);

printf("please enter the flag(0 or 1):");

scanf("%d", &flag);

bit_set(&P_data, pos, flag);

printf("the number is %d\n", P_data);

return 0;

}c语言常见的置0和置1操作,【c语言】实现对一个8bit数据(unsigned char 类型)的指定位(例如第n位)置0或者置1操作,并保持其他位不变..._第1张图片

原文:http://blog.csdn.net/zhaoyaqian552/article/details/45852845

你可能感兴趣的:(c语言常见的置0和置1操作)