L
LLLOS.ai
Learn
L

Ask Your Tutor · Computer Science

How does endianness (byte order) affect unpacking 12-bit data?

General knowledge In depth Asked by Anonymous 23 Oct 2026, 4:55 AM IST Updated

In one line

Endianness (byte order) decides how you read and split bytes when unpacking 12-bit data from memory.

Endianness — Order of bytes in a computer word
Endianness — Order of bytes in a computer word
Diagram from Wikipedia showing big-endian and little-endian byte order.
Image: Wikipedia

The rule

Endianness is the order in which bytes are stored and read in memory—big-endian stores the most significant byte first, little-endian stores the least significant byte first.

There is no single formula, but the key is:

  • In big-endian: higher-value bytes come first.
  • In little-endian: lower-value bytes come first.

Let's understand it simply

Imagine you have a long number, like a phone number, and you write it on two slips of paper. If you give the first slip to your friend, do they expect the start of the number or the end? That’s endianness.

Now, think about a 12-bit number (like 1011 1100 1101). Computers store data in 8-bit chunks (bytes). But 12 bits don’t fit neatly into 8 bits, so the number is split across bytes. The order in which these bytes are stored and read depends on endianness.

Big-endian vs Little-endian (12-bit unpacking)
Big-endianLittle-endian
First byteHigher bitsLower bits
Second byteLower bitsHigher bits
Example order[0xAB, 0xCD, 0xEF][0xCD, 0xAB, 0xEF]

How it works

  1. Packing 12-bit data:
  • You have a stream of 12-bit numbers (like sensor readings).
  • Each 12-bit value must be split across bytes (since 1 byte = 8 bits).
  1. Storing in memory:
  • The bytes are stored in a certain order: big-endian or little-endian.
  1. Unpacking (reading) the data:
  • You read the bytes from memory.
  • You must know the endianness to combine the right bits from each byte.
  1. Big-endian unpacking:
  • The first byte you read contains the higher bits of the 12-bit value.
  • The next byte gives the lower bits.
  1. Little-endian unpacking:
  • The first byte gives the lower bits.
  • The next byte gives the higher bits.
  1. If you get the order wrong:
  • You combine the wrong bits, and the value becomes garbage.
Unpacking 12-bit data (big-endian)
  1. Read first byte (higher 8 bits)
  2. Read next 4 bits from second byte (lower 4 bits)
  3. Combine to get first 12-bit value
  4. Read last 4 bits of second byte (higher 4 bits of next value)
  5. Read next byte (lower 8 bits)
  6. Combine to get second 12-bit value

The story behind it

The idea of endianness comes from computer architecture. Different processors (like Intel and ARM) chose different byte orders for historical reasons. The term "endianness" was inspired by a story in Jonathan Swift’s "Gulliver’s Travels" about people fighting over which end of an egg to break. In computers, it’s about which end of the data you read first.

Where you see it in real life

  • Reading sensor data:
  • Many sensors (like temperature or ECG) send 12-bit values. If you unpack them with the wrong endianness, your readings are wrong.
  • Image files:
  • Some image formats use 12-bit pixels. Software must know the byte order to display the image correctly.
  • Networking:
  • Data sent over the internet often uses big-endian. If your computer is little-endian, you must swap the bytes.
  • Embedded systems:
  • Microcontrollers in cars or washing machines may use 12-bit data and have a fixed endianness.
  • Audio files:
  • Some audio formats use 12-bit samples. Playing them with the wrong byte order makes the sound noisy.
  • Medical equipment:
  • Devices like ECG machines store patient data in 12-bit chunks. Wrong unpacking can lead to wrong diagnosis.

Why it matters

  • Correct data interpretation:
  • If you use the wrong endianness, your numbers are wrong—like reading a cricket score backwards.
  • Software compatibility:
  • Programs must handle endianness to work across different computers.
  • Data sharing:
  • When sharing files between systems, you must agree on endianness.
  • Exams and interviews:
  • Questions on endianness are common in computer science exams and job interviews.

Worked example

Let’s say you have two 12-bit numbers packed into three bytes:

Suppose the numbers are:

  • First: 0xABC (1010 1011 1100)
  • Second: 0xDEF (1101 1110 1111)

Packed in memory (big-endian):

  • Byte 1: 1010 1011 (0xAB)
  • Byte 2: 1100 1101 (0xCD)
  • Byte 3: 1110 1111 (0xEF)

Unpacking (big-endian):

Given: Bytes = [0xAB, 0xCD, 0xEF]
Step 1: First 12 bits = (0xAB << 4) | (0xCD >> 4)
Step 2: (0xAB << 4) = 0xAB0 = 2736
Step 3: (0xCD >> 4) = 0xC = 12
Step 4: First value = 2736 | 12 = 2748 (0xABC)
Step 5: Second 12 bits = ((0xCD & 0x0F) << 8) | 0xEF
Step 6: (0xCD & 0x0F) = 0xD = 13
Step 7: (13 << 8) = 3328
Step 8: Second value = 3328 | 239 = 3567 (0xDEF)

If you use little-endian, the order of combining bits changes.

Common mistakes

  • Ignoring endianness:
  • Assuming all systems use the same byte order. Always check!
  • Mixing up bit and byte order:
  • Endianness is about bytes, not individual bits.
  • Wrong bit shifts:
  • Shifting the wrong number of bits when combining bytes.
  • Forgetting to mask bits:
  • Not using bitwise AND (&) to get only the needed bits.

Remember it

Endianness is like reading a number from left or right—get it wrong, and your answer is upside down.

Check yourself

What is endianness in computers?

It is the order in which bytes are stored and read in memory.

Why does endianness matter when unpacking 12-bit data?

Because it tells you how to combine bytes to get the correct value; wrong order gives wrong data.

If you mix up big-endian and little-endian, what happens?

You get the wrong numbers, because you combine the wrong parts of bytes.

How would you unpack the first 12-bit value from bytes 0xAB and 0xCD in big-endian?

Take 0xAB as higher 8 bits, upper 4 bits of 0xCD as lower 4 bits, combine them.

Quick recap

  • Endianness decides the order of bytes in memory.
  • Unpacking 12-bit data needs careful bit manipulation.
  • Big-endian: higher bits come first; little-endian: lower bits come first.
  • Wrong endianness gives wrong data.
  • Always check the data format before unpacking.
Written by the LLOS.ai tutor. Asked by Anonymous on 23 Oct 2026, 4:55 AM IST