Be Careful When Using Bit Field
Below illustration is based on MSP430, IAR. See the implementation below.
typedef struct
{
uint8_t groupAddress :; //group address
uint8_t pointAddress :; //point address
uint8_t deviceType :; //device type;
}DeviceAddressS; gDeviceAddress.groupAddress = address >> ;
gDeviceAddress.pointAddress = address & 0x7;
Let’s check the assemble code, there are total 26 execution codes and 28 execution codes for two lines C source code.

If we don’t use the bit field, what will be happened?
typedef struct
{
uint8_t pointAddress; //point address
uint8_t groupAddress; //group address
uint8_t deviceType; //device type;
}DeviceAddressS;
gDeviceAddress.groupAddress = address >> ;
gDeviceAddress.pointAddress = (address & 0x7);
Checking the assemble code again, there are total 10 execution codes and 12 execution codes.

So, the rule is that “be careful when using bit field”. Do you really has no enough space? In fact, if you don’t have enough space, the MCU performance is low, so don’t use bit field normally. If you real want to use it, using union and assigning whole value at the same time, see example below.
typedef union
{
uint8_t value;
struct
{
uint8_t pointAddress :; //point address
uint8_t groupAddress :; //group address
}bitFields;
}AddressU; void AddressTest(void)
{
uint8_t adddress = ;
AddressU temp;
temp.value = adddress;
temp.value |= (adddress & 0x7);
}

It has a good performance.
Note that, they have same performance when reading for bit field or non-bit field.
Be Careful When Using Bit Field的更多相关文章
- Sample Credential Providers
Windows Vista Sample Credential Providers Overview Contents Terms of Use Release Notes SampleCre ...
- How to reduce Index size on disk?减少ES索引大小的一些小手段
ES索引文件瘦身总结如下: 原始数据:(1)学习splunk,原始data存big string(2)原始文件还可以再度压缩倒排索引:(1)去掉不必要的倒排索引信息:例如文件位置倒排._source和 ...
- salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)
本篇引用以下三个链接: http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html?m=1 https://github ...
- salesforce 零基础学习(五十八)通过sObject的field返回其对应的基础类型
项目中有时候会要求通过sObject的Field的type类型返回其对应的基本类型,然后对其进行相关的处理,创建sObject的field可以选择的type类型是固定多的. 上述类型可以转换成几种基本 ...
- SharePoint 2013 Create taxonomy field
创建taxonomy field之前我们首先来学习一下如果创建termSet,原因是我们所创建的taxonomy field需要关联到termSet. 简单介绍一下Taxonomy Term Stor ...
- 代码的坏味道(7)——临时字段(Temporary Field)
坏味道--临时字段(Temporary Field) 特征 临时字段的值只在特定环境下有意义,离开这个环境,它们就什么也不是了. 问题原因 有时你会看到这样的对象:其内某个实例变量仅为某种特定情况而设 ...
- 【转】Django Model field reference学习总结
Django Model field reference学习总结(一) 本文档包含所有字段选项(field options)的内部细节和Django已经提供的field types. Field 选项 ...
- go-使用 unsafe 修改 struct 中的 field 的值
以下是方法,不要纠结原理,等东西积累多了,你才有能力纠结原理: 首先,你需要有一个这样的函数,这是在 nsq 的源码里直接抄过来的: func unsafeValueOf(val reflect.Va ...
- MongoDB查询转对象是出错Element '_id' does not match any field or property of class
MongoDB查询转对象是出错Element '_id' does not match any field or property of class 解决方法: 1.在实体类加:[BsonIgno ...
随机推荐
- 7.2 C++模板类实例化
参考:http://www.weixueyuan.net/view/6399.html 总结: array < int >表明用int类型来代替模板类中的类参数“T”,编译器会将模板类ar ...
- Python的string模块
如果要使用string模块,需要先导入该模块 import string string.ascii_lowercase #打印所有的小写字母 string.ascii_uppercase #打印所 ...
- Spring学习四
1先导入 Asject的jar包 2配置文件加入标签 ,并加入 <aop:aspectj-autoproxy proxy-target-class="true">(如果 ...
- delete CDU
function DeletePDU(){ global $person; $this->MakeSafe(); // Do not attempt anything else if the l ...
- 【转】Delphi 10.3关于相机该注意的细节
感谢移动信息化专家提供的方法,他的ChinaCock组件是相当的专业,感兴趣可以加入qq群223717588.
- centos安装python3虚拟环境和python3安装
1.本文的系统命令一般会在语句前加上#号,以区分系统命令及其他内容.输入命令时,无需输入#号. # yum install vim 2.本文系统输出的信息,会在前面加上>>号. # whi ...
- Python 操作系统介绍 进程的创建
背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有内 ...
- Spring面向切面编程
在使用面向切面编程时,我们可以在一个地方定义通用的共鞥,但是可以通过声明的方式定义这个功能要以何种方式在何处应用,而无需修改受影响的类.横切关注点可以被模块化为特殊的类,这些类被称为切面.这样的优点是 ...
- 微软Power BI 每月功能更新系列——6月Power BI 新功能学习
Power BI Desktop 6月产品功能摘要 本月Power BI Desktop的更新,专注于图表格式化的灵活性调整,通过过滤和排序选项,数据视图现在更加强大.多个连接器也获得重大更新,其中包 ...
- 全卷积神经网络FCN
卷积神经网络CNN(YannLecun,1998年)通过构建多层的卷积层自动提取图像上的特征,一般来说,排在前边较浅的卷积层采用较小的感知域,可以学习到图像的一些局部的特征(如纹理特征),排在后边较深 ...