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.

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.
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.
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.
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.
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.
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.
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.
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.
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.
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?
Show answer
Answer: A ANDing with a mask reads the selected bit while leaving the unrelated flags untouched.
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?
Show answer
Answer: A Big-endian places the most significant byte first, so 01 then 90 gives 0x0190.
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?
Show answer
Answer: C OR with a single-bit mask sets bit 2 while every other bit keeps its current value.
Print reference
Answers
Answer key.
- A · ANDing with a mask reads the selected bit while leaving the unrelated flags untouched.
- A · Big-endian places the most significant byte first, so 01 then 90 gives 0x0190.
- C · OR with a single-bit mask sets bit 2 while every other bit keeps its current value.