码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-06-13 14:17:35      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:


给定函数原型:void bit_set(unsigned char *p_data,unsigned char positin,int flag)

参数说明:p_data是指定的源数据;position是指定位(取值范围为1~8);flag表示置0还是置1操作。


#include <stdio.h>
void bit_set(unsigned char *p_data, unsigned char position, int flag)
{
    int a = 1<<(position-1);
    if (flag)
    {
        *p_data |= a;
    }
    else
    {
        *p_data &= ~a;
    }
}
int main()
{
    int a = 127;
    bit_set(&a,8,1);
    printf("%d\n",a);
    return 0;
}

技术分享

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

标签:

原文地址:http://blog.csdn.net/sulijuan66/article/details/46482189

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!