您的位置:首页 > 编程语言 > C语言/C++

c语言 对8bit位数据的某位置1或 0

2015-09-26 18:40 316 查看
/*
对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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: