When you are staring at a raw I2C register dump or reading a 12-bit ADC value off an ESP32, you need to translate those high and low logic states into a base-10 number you can actually use. The binary to decimal conversion formula is the mathematical bridge between physical hardware states and human-readable values.
The direct answer for an unsigned integer is the sum of each bit multiplied by its positional weight: N10 = Σ (di × 2i). Below, we will break down every symbol, track the positional 'units' through solved problems, and look at the exact bench mistakes that happen when you misapply it.
The Core Formula and Symbol Spec-Sheet
For any unsigned binary word, the decimal equivalent is calculated by summing the products of each binary digit and its corresponding base-2 exponential weight.
N10 = Σi=0n-1 (di × 2i)
Here is the exact spec-sheet definition for every symbol in that equation:
| Symbol | Definition | Bench Context |
|---|---|---|
| N10 | Final decimal value (base-10 integer) | The voltage, address, or sensor reading you want. |
| di | The binary digit (0 or 1) at position i | The physical logic HIGH (1) or LOW (0) on a pin. |
| i | The zero-based positional index | Starts at 0 for the Least Significant Bit (LSB). |
| n | Total number of bits in the binary word | 8 for a standard byte, 12 for an ESP32 ADC, 16 for I2C. |
| 2i | The positional weight (the 'unit' multiplier) | The actual decimal value that specific bit adds if HIGH. |
Assumptions, Bounds, and the Index Mistakes That Break Your Code
When the Formula Applies (and When It Doesn't)
This specific summation formula applies strictly to unsigned integers. If you are reading a signed 16-bit temperature sensor (like the TMP117) that uses Two's Complement, this formula will give you the wrong answer for negative temperatures. For Two's Complement, if the Most Significant Bit (MSB) is 1, you must subtract 2n from your final N10 result.
The 'Unit' Mistakes That Ruin Conversions
In physics, a unit mistake means confusing meters for millimeters. In base conversion, a 'unit mistake' means misidentifying the positional weight unit (2i). The two most common fatal errors on the bench are:
- 1-Based Indexing: Treating the LSB as position i=1 (weight 21 = 2) instead of i=0 (weight 20 = 1). This instantly doubles your final decimal value and shifts everything left.
- Physical vs. Logical MSB Confusion: Datasheets often label physical pins as 1 through 8. Pin 1 is not always logical bit 0 (the LSB). On many parallel DACs, Pin 1 is the MSB (27). If you map physical pin 1 to formula index i=0, your output will be completely inverted.
Realistic Answer Magnitudes
Before you calculate, you should know the realistic bounds of your n-bit system to sanity-check your answer. As a rule of thumb for embedded systems:
- 8-bit: Max 255 (I2C addresses, standard PWM duty cycles)
- 10-bit: Max 1023 (Legacy Arduino Uno ADC)
- 12-bit: Max 4095 (ESP32 ADC, modern DACs)
- 16-bit: Max 65,535 (High-res sensors, timer counters)
Step-by-Step Solved Problems: Tracking the Weight Units
Let's run through two real-world scenarios, explicitly tracking the 'weight unit' for every single bit to prevent indexing errors.
Problem 1: Reading an 8-bit I2C Port Expander (PCF8574)
Scenario: You read an 8-bit register from a PCF8574 I/O expander. The raw binary byte is 10110100. What is the decimal state value?
Setup: n = 8. The rightmost bit is the LSB (i=0). The leftmost is the MSB (i=7).
| Bit Position (i) | Binary Digit (di) | Weight Unit (2i) | Product (di × 2i) |
|---|---|---|---|
| 7 (MSB) | 1 | 128 | 128 |
| 6 | 0 | 64 | 0 |
| 5 | 1 | 32 | 32 |
| 4 | 1 | 16 | 16 |
| 3 | 0 | 8 | 0 |
| 2 | 1 | 4 | 4 |
| 1 | 0 | 2 | 0 |
| 0 (LSB) | 0 | 1 | 0 |
Summation: 128 + 0 + 32 + 16 + 0 + 4 + 0 + 0 = 180.
Sanity Check: 180 is less than the 8-bit maximum of 255. The math holds.
Problem 2: ESP32 12-bit ADC Voltage Calculation
Scenario: You are reading a 12-bit ADC channel on an ESP32-S3 via the ESP-IDF oneshot API. The raw binary register dump is 0110 1001 1100. The reference voltage is 3.3V. What is the decimal ADC count, and what is the voltage?
Setup: n = 12. LSB is i=0.
- Calculate Decimal Count (N10):
- Bit 11: 0 × 2048 = 0
- Bit 10: 1 × 1024 = 1024
- Bit 9: 1 × 512 = 512
- Bit 8: 0 × 256 = 0
- Bit 7: 1 × 128 = 128
- Bit 6: 0 × 64 = 0
- Bit 5: 0 × 32 = 0
- Bit 4: 1 × 16 = 16
- Bit 3: 1 × 8 = 8
- Bit 2: 1 × 4 = 4
- Bit 1: 0 × 2 = 0
- Bit 0: 0 × 1 = 0
- Calculate Voltage:
Voltage = (N10 / 2n) × Vref = (1692 / 4096) × 3.3V = 1.363V.
Note: As detailed in Texas Instruments' data converter application notes, real-world ADC readings require calibration offsets, but the base-2 conversion remains the foundational first step.
Rearranging the Formula: Solving for Bits, Max Values, and Specific Positions
You won't always be going from binary to decimal. Often, you need to work backward from a decimal requirement to figure out your hardware constraints. Here are the three most useful rearranged forms of the core formula.
1. Solving for Maximum Decimal Value (Nmax)
When all bits are HIGH (di = 1), the geometric series collapses to:
Nmax = 2n - 1
Use case: Sizing a variable type. If you need to store a sensor reading that peaks at 4000, an 8-bit integer (max 255) will overflow. You need a 16-bit integer (max 65,535).
2. Solving for Required Bit Width (n)
If you know the maximum decimal value you need to represent, you can find the minimum number of bits required by rearranging with a base-2 logarithm:
n = ⌈ log2(Nmax + 1) ⌉
Use case: You are designing a custom PCB with a DIP switch array to select one of 50 different motor profiles. log2(51) = 5.67. Rounding up (the ceiling function), you need exactly 6 physical switches.
3. Extracting a Specific Bit (dk) from a Decimal
If you have a decimal number and need to know if a specific bit at position k is HIGH or LOW (useful for bitwise masking in C++):
dk = ⌊ N10 / 2k ⌋ mod 2
Use case: In firmware, instead of using this math directly, we use the bitwise right-shift operator: (N_10 >> k) & 1. This is the programmatic equivalent of the rearranged formula.
Bench Walkthrough: The DMX512 Addressing Disaster
Formulas don't exist in a vacuum; they exist on workbenches where silk-screen labels fade and datasheets get misread. Here is a real-world scenario where ignoring the physical-to-logical mapping of the formula caused hours of debugging.
The Setup
A technician is configuring a legacy DMX512 stage lighting controller. DMX addresses are set using a 9-position physical DIP switch block on the back of the fixture. The target DMX address for this specific light is 256.
The Numbers
Using the rearranged formula for bit extraction, the tech calculates the binary representation of decimal 256.
256 = 1 × 28.
Therefore, logical bit i=8 must be HIGH (1), and all other bits (i=0 through i=7) must be LOW (0).
The Outcome
The tech flips Switch #1 to the ON position, assuming Switch #1 corresponds to the first bit. They power up the rig. The lighting console sends DMX data to address 256, but the fixture does not respond. Instead, the fixture randomly triggers when the console is sending data to address 1.
What Went Wrong: The Physical vs. Logical Trap
The tech fell victim to a 1-based indexing and physical mapping error.
- The Assumption: The tech assumed physical Switch #1 was logical bit i=0 (weight 1), and physical Switch #9 was logical bit i=8 (weight 256).
- The Reality: The manufacturer wired the DIP switch block MSB-first. Physical Switch #1 was hardwired to logical bit i=8 (weight 256), and physical Switch #9 was wired to logical bit i=0 (weight 1).
By flipping physical Switch #1 ON, the tech actually set logical bit i=8 HIGH, which should have yielded 256. Wait—why did it trigger on address 1? Because the tech also read the silk-screen label upside down, and flipped the switch block physically located at the bottom (Switch 9, weight 1) thinking it was Switch 1.
The Bench Lesson: The binary to decimal conversion formula is mathematically flawless. The failure point is almost always the hardware interface. Always verify which physical pin or switch corresponds to i=0 (the LSB) by checking the schematic or probing with a multimeter before trusting the silk-screen labels. A logical '1' only means something when you know exactly which copper trace it lives on.






