/* 对8bit位数据的某位置1或 0 例:00001010 对第二位制零 00001000 */ #include<stdio.h> #include<stdlib.h> void bit_set_1_0(unsigned char *p_data,unsigned char position,int flag) { if (position > 8||position<1 || p_data == NULL || flag<0 || flag>1) { printf("输入不合法\n"); exit(-1); } if ((*p_data >> (position - 1) & 1 )!= flag) { *p_data = *p_data ^ (1 << (position-1)); } } int main() { unsigned char ch = 5; bit_set_1_0(&ch,6,1); printf("%d",ch); return 0; }
时间: 2024-10-13 06:19:59