Jul 8, 2024 Binary and hex are easy!

Summary

 

John Cotton and I pair up for a knowledge-sharing session to determine the status of a hardware sensor in a Nerves firmware project. I walk through the sensor's status register in the datasheet and explain how to work with the data in binary and hex in Elixir. Hex values become easily understandable and amazing to work with once you know the trick.

First, we break down the status register in the datasheet, identifying the bits we need to consider as errors and the bits we can ignore. Then we convert the binary value to decimal since decimal is commonly seen in programming when comparing numbers. Knowing the bits we don't care about, we create a bit mask to ignore them. However, decimal values are difficult to think in when creating bit masks, so we turn to a binary representation of the data in the source code. Binary becomes verbose, so I teach a trick to quickly convert between binary and hex mentally. This makes the code very concise, yet still understandable.