L
LLLOS.ai
Learn
L

Ask Your Tutor · Computer Science

How do you write Python code to unpack 12-bit data?

General knowledge Asked by Anonymous 22 Oct 2026, 11:22 PM IST Updated

In one line

To unpack 12-bit data in Python, you split a stream of bytes into separate 12-bit numbers.

The rule

Unpacking 12-bit data means extracting each 12-bit value from a packed byte sequence.

There is no single formula, but the idea is: read every 3 bytes (24 bits), and split them into two 12-bit numbers.

Let's understand it simply

Imagine you have a box of ladoos, but each ladoo is cut into 12 pieces. The shopkeeper packs them in a way that every 3 pieces (bytes) actually hold 2 full ladoos (12-bit numbers). To eat a full ladoo, you need to carefully take out the right 12 pieces from the packed box.

In computers, data is often packed tightly to save space. But most computers use 8 bits (1 byte) as the basic unit. So, 12-bit data doesn't fit neatly into bytes. We have to do some cutting and joining to get each 12-bit number out.

How it works

  1. Read the packed data as bytes. Each byte is 8 bits.
  2. Take 3 bytes at a time (24 bits in total).
  3. The first 12 bits (from these 24) make the first number.
  4. The next 12 bits make the second number.
  5. Repeat for the whole data stream.
  6. Use bitwise operations (shifting and masking) to extract the numbers.
Unpacking 12-bit data from bytes
  1. Take 3 bytes (24 bits) from the data
  2. First 12 bits → first number
  3. Next 12 bits → second number
  4. Repeat for the rest of the data

Where you see it in real life

  • Digital cameras: Raw image sensors often use 12-bit values for each pixel.
  • Medical devices: Some ECG or X-ray machines store readings as 12-bit samples.
  • Audio equipment: Some old sound cards use 12-bit audio samples.
  • Embedded systems: Sensors in cars or factories may send 12-bit data to save space.

Why it matters

  • Saves memory: Packing 12-bit data uses less space than 16 bits per value.
  • Required for reading sensor data: Many devices send data in 12-bit format.
  • Needed for image processing: To work with raw photos, you must unpack 12-bit pixels.
  • Useful in competitive programming: Sometimes, problems give you packed data to test your skills.

Worked example

Let's say you have these 3 bytes: 0xAB, 0xCD, 0xEF. You want to unpack them into two 12-bit numbers.

```python

Step 1: Combine the 3 bytes into a 24-bit number

packed = (0xAB << 16) | (0xCD << 8) | 0xEF

packed = 0xAB CDEF

Step 2: Extract the first 12 bits (top 12 bits)

first = (packed >> 12) & 0xFFF # 0xABC

Step 3: Extract the next 12 bits (bottom 12 bits)

second = packed & 0xFFF # 0xDEF

Step 4: Print the results

print(hex(first), hex(second)) # Output: 0xabc 0xdef ```

Common mistakes

  • Forgetting to use bitwise AND (& 0xFFF) to keep only 12 bits.
  • Not reading the correct number of bytes (should be multiples of 3).
  • Mixing up byte order (big-endian vs little-endian).
  • Assuming 12 bits fit in a single byte (they don't).

Remember it

Every 3 bytes hold 2 numbers: split 24 bits into two 12-bit values.

Check yourself

Why do we take 3 bytes at a time when unpacking 12-bit data?

Because 3 bytes (24 bits) can be split into two 12-bit numbers.

What does '& 0xFFF' do in the code?

It keeps only the lowest 12 bits, removing any extra bits.

What happens if you try to unpack 12-bit data from only 2 bytes?

You won't have enough bits (only 16), so you can't get two full 12-bit numbers.

Quick recap

  • 12-bit data is packed across bytes, not fitting neatly into 8-bit units.
  • Take 3 bytes at a time to get two 12-bit numbers.
  • Use bitwise shifting and masking to extract each 12-bit value.
  • Common mistake: forgetting to mask with 0xFFF to keep only 12 bits.
Written by the LLOS.ai tutor. Asked by Anonymous on 22 Oct 2026, 11:22 PM IST