/*请编写实现以下功能函数:实现对一个8bit数据(unsigned char)的指定位(例如第8位)的置0或置1操作,并保持其他位不变。
函数原型:void bit_set(unsigned char *p_date,unsigned char position,int flag)。
函数参数说明:p_date是指定数据源,position是指定位(1~8),flag是置0或置1。
*/
#include <stdio.h>
void bit_set(unsigned char *p_date,unsigned char position,int flag)
{
unsigned char a=1;
a=a<<(position-1);
if(flag==1)
{
*p_date=*p_date|a;
}
if(flag==0)
{
a=~a;
*p_date=*p_date&a;
}
}
int main()
{
char c=10;
bit_set(&c,1,1);
printf("%d\n",c);
return 0;
}
【C语言】请编写实现以下功能函数:实现对一个8bit数据(unsigned char)的指定位(例如第8位)的置0或置1操作,并保持其他位不变
最新推荐文章于 2023-06-25 15:39:54 发布