IoT Fundamentals · Study deck

Bitwise Operations and Endianness

Picture a configuration write that enables an alarm but also changes the sensor range.

Physics Phoebe is your guide for this deck.

databitwiseoperations
Physics Phoebe, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Explain: This supports the bitwise chapter's contract argument: multi-byte fields need an explicit byte order beside width, signedness, scale, and version before shifts or masks can produce trustworthy values.
  • Explain: A row of light switches is the other half of the picture: a mask is the list of switches you are allowed to touch, leaving the rest alone.
  • Explain: It should apply the documented masks, test each flag, and leave reserved bits untouched so future firmware can add meanings without breaking old readers.
  • Explain: Firmware means the program stored on a device to control its hardware.
Real IoT code uses AND to read, OR to set, XOR to toggle, and shifts to pack or extract bit fields.
Real IoT code uses AND to read, OR to set, XOR to toggle, and shifts to pack or extract bit fields.
iotclass.org

Major section

In 60 Seconds

The compact byte hides several settings that must be kept separate.

  • Firmware means the program stored on a device to control its hardware.
  • A payload means the part of a message that carries the application data.
  • The safe habit is to treat byte order, bit positions, width, scaling, units, and reserved bits as one documented contract.
iotclass.org

Major section

Start With the Story

You will decode multi-byte fields, extract packed flags, and update selected register bits without disturbing their neighbours.

  • Several flags first share one byte, so an order or mask error can reach a valid but unintended bit.
  • The unchanged neighbours in the last panel are the decisive test: the operation modified its target without corrupting unrelated state.
iotclass.org

Major section

Decode Bytes by Contract

A payload may carry a two-byte temperature, a four-byte timestamp, and a one-byte status register.

  • To decode it safely, both sides must agree on byte order, bit positions, scaling, and field width.
  • Two ideas do most of the work.
  • AND checks or extracts, OR sets, XOR toggles, and shifts move fields into place.

Key terms

Both results
Both results are valid integers, which is why an incorrect decoder may fail silently rather than crash.
The value 0x12345678 in big-endian (12 34 56 78) and little-endian (78 56 34 12) byte order.
The value 0x12345678 in big-endian (12 34 56 78) and little-endian (78 56 34 12) byte order.
iotclass.org

Major section

Decode Bytes by Contract (continued)

The core rule is precise: bits are numbered positions inside a byte, and a mask selects the positions you care about.

  • Any multi-byte field needs an agreed byte order before it crosses a device, gateway, file, or network boundary.
  • The same ink can read as two different numbers.
  • A row of light switches is the other half of the picture: a mask is the list of switches you are allowed to touch, leaving the rest alone.
iotclass.org

Major section

Decode Bytes by Contract (continued)

Big-endian places the most significant byte first, so hexadecimal 12 34 becomes 18×256+52=466018\times256+52=466018×256+52=4660.

  • For example, a battery-powered sensor might send one status byte where bit 0 means the temperature sample is valid, bit 2 means low battery, and bit 4 means the gateway link is ready.
  • The decoder should not judge the decimal value of the whole byte.
  • The network-byte-order note shows how canonical wire formats remove that ambiguity.
iotclass.org

Major section

Decode Bytes by Contract (continued)

It should apply the documented masks, test each flag, and leave reserved bits untouched so future firmware can add meanings without breaking old readers.

  • Both results are valid integers, which is why an incorrect decoder may fail silently rather than crash.
  • This supports the bitwise chapter's contract argument: multi-byte fields need an explicit byte order beside width, signedness, scale, and version before shifts or masks can produce trustworthy values.
  • A mask selects bitsAND with a mask reads or extracts; OR sets; AND with an inverted mask clears; XOR toggles.
iotclass.org

Major section

Under the Hood: Packed Layouts, the Pipeline, and Validation

A reliable decoder follows the same path every time, and a packed byte is only useful when its layout is documented and tested.

  • Packed Status BytesPacking flags into one byte is common when every byte must be justified.
  • The humidity sample is ready for use.
  • The next uplink can be attempted.
The decode pipeline: load, order, mask and shift, scale, then validate.
The decode pipeline: load, order, mask and shift, scale, then validate.
iotclass.org

Major section

Under the Hood: Packed Layouts, the Pipeline, and Validation (continued)

The measured supply is below the warning threshold.

  • Include a normal example and each boundary value; test invalid and reserved-bit states.
  • Common PitfallsSending raw multi-byte memory: Memory layout can differ from the wire format; assemble values explicitly before crossing a boundary.
  • In production, bitwise work is precise engineering.
iotclass.org

Major section

Checkpoint: Summary

Endianness defines how a multi-byte value is assembled; resolve it before reading any number.

  • Masks select the bits that carry meaning: AND tests or extracts, OR sets, AND with an inverted mask clears, and XOR toggles.
  • Packed payloads need documentation, a worked example, and decoder tests kept with the source.
  • Validation catches wrong byte order, wrong field width, reserved-bit violations, and impossible states.
iotclass.org

Deck summary

Key takeaways

The compact byte hides several settings that must be kept separate.

  • You will decode multi-byte fields, extract packed flags, and update selected register bits without disturbing their neighbours.
  • A payload may carry a two-byte temperature, a four-byte timestamp, and a one-byte status register.
  • The core rule is precise: bits are numbered positions inside a byte, and a mask selects the positions you care about.
  • Big-endian places the most significant byte first, so hexadecimal 12 34 becomes 18×256+52=466018\times256+52=466018×256+52=4660.
iotclass.org

Retrieval practice

Recall check 1 of 3

Physics Phoebe says: answer from memory, then check your reasoning.

Q1A device packs eight boolean status flags into one byte. What should firmware use to test one flag without changing the others?

AConvert the byte to decimal and guess from the number's size
BOverwrite the whole byte with the flag value
CSend eight separate text strings for every status update
DMask the target bit before interpreting it.
Show answer

Answer: D ANDing with a mask reads the selected bit while leaving the unrelated flags untouched.

iotclass.org

Retrieval practice

Recall check 2 of 3

Physics Phoebe says: answer from memory, then check your reasoning.

Q2A decoder accepts a packed status field. Which test set best checks that its layout will survive a firmware change?

ATest normal values repeatedly against the current firmware.
BTest a packet from the previous firmware version using the current decoder.
CTest the decoded display at several routine operating points.
DNormal values, both limits, and a case with a reserved bit set.
Show answer

Answer: D The limits and reserved state reveal parsing drift that a normal sample can hide.

Q3A protocol says a 16-bit field is big-endian. The received bytes are 01 90. What raw integer should the decoder assemble before scaling?

A0x9001
B0x0109
C0x1900
D0x0190
Show answer

Answer: D Big-endian places the most significant byte first, so 01 then 90 gives 0x0190.

iotclass.org

Retrieval practice

Recall check 3 of 3

Physics Phoebe says: answer from memory, then check your reasoning.

Q4Firmware must enable only bit 2 of a shared control register without disturbing the other settings. Which operation is correct?

Areg = (1u << 2), because direct assignment sets only the target bit
Breg &= (1u << 2), because AND selects the target bit with a mask
Creg |= (1u << 2), because OR sets bit 2 while preserving neighbors
Dreg ^= (1u << 2), because XOR changes the selected bit while preserving neighbors
Show answer

Answer: C OR with a single-bit mask sets bit 2 while every other bit keeps its current value.

iotclass.org

Print reference

Answers

Answer key.

  1. D · ANDing with a mask reads the selected bit while leaving the unrelated flags untouched.
  2. D · The limits and reserved state reveal parsing drift that a normal sample can hide.
  3. D · Big-endian places the most significant byte first, so 01 then 90 gives 0x0190.
  4. C · OR with a single-bit mask sets bit 2 while every other bit keeps its current value.
iotclass.org