阅读背景:

实现一个对8bit数据指定某一位置0或1

来源:互联网 
<pre name="code" class="cpp">
 
 
方法一
#include<stdio.h>#include<math.h>void bit_set(unsigned char *p_date, unsigned char position, int flag){char  a =(char) pow(2, (position - 1)); //指数 2^(position - 1)if (flag == 1){*p_date |= a;//0000 0010或0000 0001   |表示按位或}               // 0000 0011else     {*p_date &= ~a; //0000 0010&1111 1110  ~表示按位取反}                 // 0000 0010            &表示按位与}int main(){unsigned char val = 2;bit_set(&val, 1, 0);printf("%d\n", val);getchar();return 0;}#inclu



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: