实现对一个8bit数据的指定位的置0或者置1操作,并保持其他位不变。

给定函数原型:void bit_set(unsigned char *p_data,unsigned char positin,int flag)
参数说明:p_data是指定的源数据;position是指定位(取值范围为1~8);flag表示置0还是置1操作。
 1 #include <stdio.h>
 2 void bit_set(unsigned char *p_data, unsigned char position, int flag)
 3 {
 4     int a = 1<<(position-1);
 5     if (flag)
 6     {
 7         *p_data |= a;
 8     }
 9     else
10     {
11         *p_data &= ~a;
12     }
13 }
14 int main()
15 {
16     int a = 127;
17     bit_set(&a,8,1);
18     printf("%d
",a);
19     return 0;
20 }

原文地址:https://www.cnblogs.com/-zyj/p/5794008.html