A robot inspection service can pack one Boolean result per region into a few bytes. The receiving service needs to know how many flags the sender recorded and which position within each byte corresponds to the first flag. When either detail is implicit, a message can decode successfully while assigning a result to the wrong region.

A local fixture used ten invented inspection flags and NumPy 2.5.2. The fixture exercised only array packing and unpacking. The flag values describe a teaching example. They carry no measured defect rate or product disposition.

Map ten flags into two bytes

Assume each flag marks whether a region contributes evidence to a later review. The flag array follows the manifest order from the first region to the tenth. The two supported bit orders place the same input flags in different positions inside each byte.

import numpy as np

flags = np.array([1, 0, 1, 1, 0, 0, 1, 0, 1, 0], dtype=np.uint8)

big = np.packbits(flags, bitorder="big")

little = np.packbits(flags, bitorder="little")

The fixture produced hexadecimal B2 80 for big bit order and 4D 01 for little bit order. In the big case, the first input flag goes into the most significant bit of the first byte. The ninth flag goes into the most significant bit of the second byte. In the little case, those flags occupy the least significant position of their respective bytes. The first packed byte still contains flags one through eight under either convention.

This is a choice about bit position within a byte. A format that also carries multi-byte sequence numbers or timestamps should specify their byte order separately. The reader should be able to map a region index to a byte and bit position without inferring a rule from the host machine.

Recover the declared number of flags

Ten flags leave six unused bits in the final byte. NumPy's packbits fills those positions with zeros. The original flag count therefore belongs in the schema or in a field that travels with the payload. Unpacking the entire two-byte buffer in the fixture returned sixteen values, including the six padding zeros. A value of zero can also be a valid inspection flag, so its value alone cannot show whether it was part of the original array.

decoded = np.unpackbits(big, count=flags.size, bitorder="big")

assert np.array_equal(decoded, flags)

assert np.array_equal(

np.unpackbits(little, count=flags.size, bitorder="little"), flags

)

The same bytes can produce a different set of ten flags when decoded with the wrong bit order. In the fixture, reading B2 80 with little bit order returned [0, 1, 0, 0, 1, 1, 0, 1, 0, 0]. It still returned ten values. A validation rule that only checks decoded length would accept that result.

Check the byte count before decoding

NumPy's unpackbits can fill the output with zeros when the requested count exceeds the available input bits. This allows a decoder to produce the requested length even when a byte is missing. In the fixture, retaining only B2 and requesting ten flags returned a zero for the ninth flag, which was originally one. The decoder had no second byte from which to recover the original value.

For an exact-length payload format, use a trusted flag count and verify the packed size first. If the payload includes headers or multiple sections, apply that check to the extracted flag section after validating its bounds.

expected_bytes = (flag_count + 7) // 8

if len(payload) != expected_bytes:

raise ValueError("packed flag section has the wrong length")

packed = np.frombuffer(payload, dtype=np.uint8)

flags = np.unpackbits(packed, count=flag_count, bitorder=bit_order)

The example assumes flag_count is a validated, nonnegative integer and bit_order comes from the format version. Set sensible bounds on the declared flag count before interpreting external data. A separate rule can verify that reserved padding positions are zero if the format requires canonical bytes. Keep that rule distinct from the check for missing bytes. Payload length alone also cannot detect changed contents, so retain any integrity check supplied by the transport.

Preserve region order when the layout changes

An export may group flags by camera, product variant, or inspection station. Each grouping needs a stable index map. If a new product adds a region or the camera export changes its ordering, update the schema version and keep the old map with historical messages. Byte-level agreement can coexist with different meanings when the two services disagree about which region each flag represents.

For arrays with several dimensions, record the packing axis and the corresponding shape. NumPy flattens the input when axis is omitted. Packing each row separately can add padding to every row, whereas packing a flattened array adds padding only at the end. A receiver needs the same layout convention before reconstructing its region map.

Test what a successful decode can hide

Keep a reference fixture with exact expected bytes in the tests shared by the sender and receiver. Use both an alternating pattern and a non-symmetric byte such as B2 so a reversed bit order changes the expected result. Include a length that leaves padding, a valid final zero flag, and a missing final byte. Exercise the case where two different region maps use the same byte sequence.

A practical inspection receipt can record the format version, region-map identifier, declared flag count, bit order, expected byte count, and read outcome. The receipt shows how the packed message became region-level evidence and gives a technician enough information to reproduce a discrepancy.

Sources checked November 28, 2026

NumPy, numpy.packbits

https://numpy.org/doc/stable/reference/generated/numpy.packbits.html

NumPy, numpy.unpackbits

https://numpy.org/doc/stable/reference/generated/numpy.unpackbits.html

Illustration. AI-generated concept based on ten synthetic inspection flags and the two observed packed-byte sequences.