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: 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.
  • 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.
  • Apply masks and shifts to extract packed status 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

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.

Why it matters

Shift next to : Network byte order keeps because it uses : Network byte order keeps to locate a communication boundary, and close on packet headers predictable, which uses packet headers predictable to show the next hand-off.

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)

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.
  • It should apply the documented masks, test each flag, and leave reserved bits untouched so future firmware can add meanings without breaking old readers.
  • A mask selects bits AND 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 Bytes Packing 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 Pitfalls Sending 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

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.

  • 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.
  • 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.
  • A reliable decoder follows the same path every time, and a packed byte is only useful when its layout is documented and tested.
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?

AApply a bit mask to isolate the target bit before interpreting it
BConvert the byte to decimal and guess from the number's size
COverwrite the whole byte with the flag value
DSend eight separate text strings for every status update
Show answer

Answer: A 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 protocol says a 16-bit field is big-endian. The received bytes are 01 90. What raw integer should the decoder assemble before scaling?

A0x0190
B0x9001
C0x0109
D0x1900
Show answer

Answer: A 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.

Q3Firmware 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. A · ANDing with a mask reads the selected bit while leaving the unrelated flags untouched.
  2. A · Big-endian places the most significant byte first, so 01 then 90 gives 0x0190.
  3. C · OR with a single-bit mask sets bit 2 while every other bit keeps its current value.
iotclass.org