# Computing stuff tied to the physical world

## Decoding bit fields – part 2

In Software on Sep 6, 2013 at 00:01

To follow up on yesterday’s post, here is some code to extract a bit field. First, a refresher: The field marked in blue is the value we’d like to extract. One way to do this is to “manually” pick each of the pieces and assemble them into the desired form: Here is a little CoffeeScript test version:

```buf = new Buffer [23, 125, 22, 2]

for i in [0..3]
console.log "buf[#{i}] = #{buf[i]} = #{buf[i].toString(2)} b"

x = buf.readUInt32LE 0
console.log "read as 32-bit LE = #{x.toString 2} b"

b1 = buf >> 7
b2 = buf
b3 = buf & 0b111
w = b1 + (b2 << 1) + (b3 << 9)
console.log "extracted from buf: #{w} = #{w.toString 2} b"

v = (x >> 15) & 0b111111111111
console.log "extracted from int: #{v} = #{v.toString 2} b"
```

Or, if you prefer JavaScript, the same thing:

```var buf = new Buffer([23, 125, 22, 2]);

for (var i = 0; i <= 3; ++i) {
console.log("buf["+i+"] = "+buf[i]+" = "+buf[i].toString(2)+" b");
}

var x = buf.readUInt32LE(0);
console.log("read as 32-bit LE = "+x.toString(2)+" b");

var b1 = buf >> 7;
var b2 = buf;
var b3 = buf & 0x7;
var w = b1 + (b2 << 1) + (b3 << 9);
console.log("extracted from buf: "+w+" = "+w.toString(2)+" b");

var v = (x >> 15) & 0xfff;
console.log("extracted from int: "+v+" = "+v.toString(2)+" b");
```

The output is:

```    buf = 23 = 10111 b
buf = 125 = 1111101 b
buf = 22 = 10110 b
buf = 2 = 10 b
read as 32-bit LE = 10000101100111110100010111 b
extracted from buf: 1068 = 10000101100 b
extracted from int: 1068 = 10000101100 b
```

This illustrates two ways to extract the data: “w” was extracted as described above, but “v” used a trick: first use built-in logic to extract an integer field which is too big (but with all the bits in the right order), then use bit shifting and masking to pull out the required field. The latter is much simpler, more concise, and usually also a little faster.

Here’s an example in Python, illustrating that second approach via “unpack”:

```import struct

buf = struct.pack('BBBB', 23, 125, 22, 2)
(x,) = struct.unpack('<l', buf)

v = (x >> 15) & 0xFFF
print(v)
```

And lastly, the same in Lua, another popular language:

```require 'struct'
require 'bit'

buf = struct.pack('BBBB', 23, 125, 22, 2)
x = struct.unpack('<I4', buf)

v = bit.band(bit.rshift(x, 15), 0xFFF)
print(v)
```

So there you go, lots of ways to extract useful data from the RF12demo sketch output!

1. Thanks for these 2 articles! I worked all weekend on a project and finally figured out what you were up to with the bit fields. I worked around this (and will now fix my code) by sending a “High Byte” and “Low Byte” for temperature and humidity from an SHT22 and reading the HEX output from Jeelink running the RF12demo.

Here’s a sample: OKX 0201BF011500 02=Node, 01=Humidity High Byte, 1B=Humidity Low Byte, 01=Temperature High Byte, 15=Temperature Low Byte, 00=Voltage Low. Translated: Node 02, Humidity 447 (i.e. 44.7%), Temperature 283 (i.e. 28.3c), Battery is ok 00.

Comments are closed.