The Decision Tree: Which Binary-to-Decimal Method Applies?

When debugging microcontroller registers or analyzing logic analyzer traces, you cannot blindly apply a single conversion formula. The correct method depends entirely on how the hardware manufacturer packed the data. Use this decision path to select the exact mathematical method for your binary-to-decimal conversion.

Data Type / Hardware Context Identifying Condition Concrete Method to Apply
Unsigned Integer (Standard GPIO, Counters) All bits represent magnitude; no sign bit defined in datasheet. Standard Positional Summation: Multiply each bit by $2^n$ and sum.
Left/Right-Aligned Packed Data (SPI/I2C ADCs) Bit-width is less than the bus width (e.g., 12-bit value on 16-bit bus). Bitwise Shift + Summation: Shift to align LSB to $2^0$, then sum.
Signed Integer (Temperature Sensors, IMU Accel) Datasheet specifies 'Two's Complement' or MSB is explicitly the sign bit. Two's Complement Inversion: If MSB=1, invert bits, add 1, sum, and apply negative sign.
IEEE 754 Floating Point (DSPs, Advanced Sensor Hubs) 32-bit or 64-bit register explicitly mapped to a float variable. IEEE 754 Extraction: Extract sign, exponent, and mantissa fields (out of scope for basic integer math).
Default Recommendation: If the datasheet is missing and you are reading a raw sensor byte, assume Two's Complement for physical measurements that can go below zero (like temperature or gyroscope drift), and Unsigned Positional Summation for absolute counts (like encoder ticks or ADC voltage levels).

Walkthrough 1: Unsigned 12-Bit ADC Register Extraction

Problem Statement: You are reading a 12-bit SAR ADC over a 16-bit SPI bus. The datasheet states the 12-bit result is left-aligned in the 16-bit register, with the lower 4 bits padded with zeros. Convert the captured 16-bit binary register value 1011 1100 1010 0000 to its actual decimal ADC count.

The Trap in This Problem

The most common exam and bench mistake is treating the entire 16-bit string as a standard unsigned integer. If you calculate the decimal value of 1011 1100 1010 0000 directly, you get 48,288. However, a 12-bit ADC has a maximum possible count of $2^{12} - 1 = 4095$. An answer of 48,288 is physically impossible for this hardware and indicates a failure to account for the left-alignment padding.

Step-by-Step Algebraic Solution

  1. Identify the padding and shift: The 12-bit value is left-aligned in a 16-bit register. This means the value is shifted left by 4 bits ($16 - 12 = 4$). To isolate the true value, we must logically shift the bits right by 4 positions.
  2. Execute the shift: Shifting 1011 1100 1010 0000 right by 4 drops the trailing zeros, yielding the true 12-bit sequence: 1011 1100 1010.
  3. Set up the positional summation: Assign powers of 2 from $2^{11}$ down to $2^0$ for the 12 bits.
    $Value = (1 \times 2^{11}) + (0 \times 2^{10}) + (1 \times 2^9) + (1 \times 2^8) + (1 \times 2^7) + (1 \times 2^6) + (0 \times 2^5) + (0 \times 2^4) + (1 \times 2^3) + (0 \times 2^2) + (1 \times 2^1) + (0 \times 2^0)$
  4. Calculate the powers of 2:
    $Value = 2048 + 0 + 512 + 256 + 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0$
  5. Sum the terms:
    $2048 + 512 = 2560$
    $2560 + 256 = 2816$
    $2816 + 128 = 2944$
    $2944 + 64 = 3008$
    $3008 + 8 = 3016$
    $3016 + 2 = 3018$

Final Answer: The decimal ADC count is 3018.

Answer Sanity Check

A 12-bit unsigned integer must fall between 0 and 4095. Our result, 3018, is within this range. Furthermore, 3018 is roughly 73.7% of the full-scale range (3018 / 4095). If this ESP32 or MCP3008 ADC is referenced to 3.3V, the measured voltage would be $3.3V \times 0.737 \approx 2.43V$, which is a highly realistic bench measurement for a biased sensor circuit.

Walkthrough 2: Signed 8-Bit Two's Complement Temperature Reading

Problem Statement: An I2C temperature sensor (like the TMP102) outputs an 8-bit signed integer representing a temperature offset in degrees Celsius. Convert the binary byte 1110 0110 to a signed decimal integer.

The Trap in This Problem

Treating the Most Significant Bit (MSB) as a standard positive magnitude bit. If you apply standard unsigned summation to 1110 0110, you get $128 + 64 + 32 + 4 + 2 = 230$. But an 8-bit signed sensor cannot read +230°C if its physical range is -55°C to +125°C. The MSB here is a sign indicator, requiring the Two's Complement theorem.

Step-by-Step Algebraic Solution

  1. Inspect the MSB (Sign Bit): The leftmost bit of 1110 0110 is 1. In two's complement, an MSB of 1 dictates that the number is negative.
  2. Invert all bits (One's Complement): Flip every 1 to 0, and every 0 to 1.
    Original: 1110 0110
    Inverted: 0001 1001
  3. Add 1 to the inverted result:
    0001 1001 + 0000 0001 = 0001 1010
  4. Convert the new positive binary to decimal:
    $Value = (1 \times 2^4) + (1 \times 2^3) + (0 \times 2^2) + (1 \times 2^1) + (0 \times 2^0)$
    $Value = 16 + 8 + 0 + 2 + 0 = 26$
  5. Apply the negative sign from Step 1:
    Final Value = -26

Final Answer: The signed decimal temperature offset is -26°C.

Answer Sanity Check

An 8-bit signed integer has a mathematical range of -128 to +127. Our result of -26 falls perfectly within this boundary. Additionally, -26°C is a valid physical temperature for a freezer or winter environmental sensor, confirming the magnitude makes real-world sense.

Independent Verification Techniques for the Lab Bench

When you are debugging live hardware and cannot rely on manual algebra, use these two methods to verify your binary-to-decimal examples independently.

Method 1: The Hexadecimal Bridge
Human brains struggle with 16-bit binary strings, but hex is trivial to group. Split the binary into 4-bit nibbles. For Walkthrough 1 (1011 1100 1010 0000), the nibbles are 1011 (B), 1100 (C), 1010 (A), 0000 (0). The hex value is 0xBCA0. Shift right by 4 bits in hex (drop the last digit) to get 0x0BCA. Plug 0xBCA into any programmer calculator to instantly verify the decimal 3018.

Method 2: Python Bitwise Verification

For two's complement verification (Walkthrough 2), use Python's ctypes library to force the hardware-level signed interpretation. This matches exactly what your C/C++ embedded firmware is doing under the hood.

import ctypes
# 0xE6 is the hex equivalent of 1110 0110
raw_byte = 0xE6 
signed_val = ctypes.c_int8(raw_byte).value
print(signed_val) # Output: -26

For deeper reading on how microcontrollers handle these specific data types at the register level, consult the All About Circuits Digital Textbook or the specific sensor datasheets from manufacturers like Texas Instruments and Microchip.

Frequently Asked Questions on Binary Conversions

Why do we add 1 after inverting the bits in Two's Complement?

Inverting the bits (One's Complement) creates a negative zero problem (both 0000 and 1111 could represent zero). Adding 1 shifts the negative scale, ensuring there is only one representation for zero and allowing standard binary addition circuits to handle both positive and negative subtraction without extra hardware logic gates.

How do I handle a 10-bit ADC value packed into a 16-bit I2C register?

Check the datasheet for alignment. If it is right-aligned, the top 6 bits are zero, and you can just run standard positional summation on the lower 10 bits. If it is left-aligned, you must shift the 16-bit value right by 6 bits before calculating the decimal sum, exactly as demonstrated in Walkthrough 1.

What happens if I read a signed two's complement byte as an unsigned integer in C++?

If you assign an 8-bit signed value (like -26, or 0xE6) to an unsigned char or uint8_t variable, the compiler will interpret the raw bits as a positive magnitude, yielding 230. This is a primary cause of erratic sensor readings in Arduino and ESP32 sketches. Always match your variable type (int8_t vs uint8_t) to the sensor's datasheet specification.