实现双8bit数据指定的位置0要么1
2015-10-19 12:05
393 查看
<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;}
方法二
#include<stdio.h>void bit_set(unsigned char *p_date, unsigned char position, int flag){if (flag == 1){*p_date |= (1 << (position - 1));//0000 0010或0000 0001} // 0000 0011else if (flag = 0){*p_date &= ~(1 << (position - 1));//0000 0010&1111 1110} // 0000 0010}int main(){unsigned char val = 2;bit_set(&val, 1, 0);printf("%d\n", val);getchar();return 0;}
相关文章推荐
- 体验一下cygwin
- 今天面试了一个刚毕业的学生
- iOS开发7-分页栏与选取器
- 25. Remove Element
- [LintCode] Longest Increasing Subsequence 最长递增子序列
- 东北四省杯经验
- easyUI 数据表格-datagrid
- 欢迎使用CSDN-markdown编辑器
- 手把手教你写Linux I2C设备驱动 tvp5158
- 基于jQury的UI框架 easyUI
- Redis学习笔记
- 深度学习(十三)caffe之训练数据格式
- oracle pl/sql之sql语法
- PostgreSQL
- JAVA使用apache http组件发送POST请求
- jQuery
- Xmanager远程连接Linux服务器图形界面--DZY
- 操作系统——进程
- php异步http请求
- IOS笔记UI--侧滑效果的实现