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. CentOS7安装cratedb

    crate: 下载: https://crate.io/download/thank-you/?download=tar crash: 下载: https://crate.io/docs/client ...

  2. Problem B 一元二次方程类

    Description 定义一个表示一元二次方程的类Equation,该类至少具有以下3个数据成员:a.b和c,用于表示方程“a*x*x + b*x +c = 0”.同时,该类还至少具有以下两个成员函 ...

  3. 一个灵活的AssetBundle打包工具

      尼尔:机械纪元 上周介绍了Unity项目中的资源配置,今天和大家分享一个AssetBundle打包工具.相信从事Unity开发或多或少都了解过AssetBundle,但简单的接口以及众多的细碎问题 ...

  4. 使用ext httpProxy代理获取列表但列表展示不全的问题解决

    今天项目中遇到一个奇葩的事情,使用ext的jsonstore通过httpproxy代理想要获取一个列表,页面显示是有五条数据的但是却只展示了2条,于是各种排查,后台确定无误后开始检查前台,发现浏览器中 ...

  5. 解决IDEA16闪退的问题

    问题描述:在编辑等使用过程中,IDEA有时会突然闪退,一脸懵逼...... 原因: 1.内存分配不够 修改home/bin/目录下面的配置文件: idea.exe.vmoptions idea64.e ...

  6. Python 静态方法

    class Person: @staticmethod # 静态方法 def yue(): print("fsadf") # 静态方法可以使用对象访问. 也可以使用类名访问. 但是 ...

  7. 计数SQL,查找单据总量和按季度查找单据总量

    --查找单据总量 select COUNT(1) as '表XXXXXX数据量' from XXXXXXwith(nolock) --按季度查找单据总量 select count(1) as '表XX ...

  8. Python基础2 字符编码和逻辑运算符

    编码 AscII码 :标准ASCII码是采用7位二进制码来编码的,最高为0,没有0000 0000,所以就是2**7-1=127个字符 , 当用1个字节(8位二进制码)来表示ASCII码时,就在最高位 ...

  9. wewqe

    script.cscript_runreason(const struct interface *ifp, const char *reason)elen = (size_t)make_env(ifp ...

  10. [转]ZooKeeper 集群环境搭建 (本机3个节点)

    ZooKeeper 集群环境搭建 (本机3个节点) 是一个简单的分布式同步数据库(或者是小文件系统) ------------------------------------------------- ...