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的更多相关文章

  1. Sample Credential Providers

        Windows Vista Sample Credential Providers Overview Contents Terms of Use Release Notes SampleCre ...

  2. How to reduce Index size on disk?减少ES索引大小的一些小手段

    ES索引文件瘦身总结如下: 原始数据:(1)学习splunk,原始data存big string(2)原始文件还可以再度压缩倒排索引:(1)去掉不必要的倒排索引信息:例如文件位置倒排._source和 ...

  3. salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)

    本篇引用以下三个链接: http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html?m=1 https://github ...

  4. salesforce 零基础学习(五十八)通过sObject的field返回其对应的基础类型

    项目中有时候会要求通过sObject的Field的type类型返回其对应的基本类型,然后对其进行相关的处理,创建sObject的field可以选择的type类型是固定多的. 上述类型可以转换成几种基本 ...

  5. SharePoint 2013 Create taxonomy field

    创建taxonomy field之前我们首先来学习一下如果创建termSet,原因是我们所创建的taxonomy field需要关联到termSet. 简单介绍一下Taxonomy Term Stor ...

  6. 代码的坏味道(7)——临时字段(Temporary Field)

    坏味道--临时字段(Temporary Field) 特征 临时字段的值只在特定环境下有意义,离开这个环境,它们就什么也不是了. 问题原因 有时你会看到这样的对象:其内某个实例变量仅为某种特定情况而设 ...

  7. 【转】Django Model field reference学习总结

    Django Model field reference学习总结(一) 本文档包含所有字段选项(field options)的内部细节和Django已经提供的field types. Field 选项 ...

  8. go-使用 unsafe 修改 struct 中的 field 的值

    以下是方法,不要纠结原理,等东西积累多了,你才有能力纠结原理: 首先,你需要有一个这样的函数,这是在 nsq 的源码里直接抄过来的: func unsafeValueOf(val reflect.Va ...

  9. 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 ...

随机推荐

  1. php安装及配置笔记

    windows下启动php-cgi方式为:php-cgi.exe -b 127.0.0.1:9000 -c php.ini(也可以是绝对路径). 安装XDebug支持,最基本的配置参数为: [xdeb ...

  2. http响应头

    If-Modified-Since标签,下图可以看出requestHeader中有If-Modified-Since

  3. <Hadoop重装><centos><NameNode失效>

    Overview 记一次真真切切的NameNode单点故障. 学校机房着火之后,刀片机上的四台服务器,唯独就NameNode彻底宕掉了,去机房看了下硬盘都坏了.. 所以只能换一个master咯.基本上 ...

  4. 1.横向滚动条,要设置两个div包裹. 2. 点击切换视频或者图片. overflow . overflow-x

    1.横向滚动条. div.1 > div.2 > img img  img 第一: 设置 div.1 一个固定的宽度 和高度  . 例如宽度 700px;  高度是 120px; 设置 o ...

  5. python day05--字典

    一.字典结构 {key:valu} 注意: key必须是不可变(可哈希)的. value没有要求.可以保存任意类型的数据. dic = {123: 456, True: 999, "id&q ...

  6. Python 黏包及黏包解决方案

    粘包现象 说粘包之前,我们先说两个内容,1.缓冲区.2.windows下cmd窗口调用系统指令 1 缓冲区(下面粘包现象的图里面还有关于缓冲区的解释) 每个 socket 被创建后,都会分配两个缓冲区 ...

  7. Strut2在Action-Result的配置文件内转到jsp页面时用URL传递参数

    Struts.2.5.5版本在Action配置文件中内有如下result,其中role是Action类中的属性,在配置文件中用到OGNL表达式 <result name="input& ...

  8. SpringBoot Maven项目 Helloworld 测试

    SpringBoot 化繁为简,简化配置 SpringBoot官方:http://projects.spring.io/spring-boot/SpringBoot使用介绍:http://blog.c ...

  9. 对jQuery ajax的认识

    1.ajax() 方法通过 HTTP 请求加载远程数据. 2.该方法是 jQuery 底层 AJAX 实现.简单易用的高层实现见 $.get, $.post 等.$.ajax() 返回其创建的 XML ...

  10. Python学习笔记第十八周

    目录: 一.JavaScript正则表达式 1.test  2.exec 二.BootStrap  1.响应式  2.图标.字体  3.基本使用 三.Django 1.安装  2.创建目录  3.进入 ...