Binary to decimal and decimal to binary conversion is the mathematical translation between the base-2 numbering system used by digital logic circuits and the base-10 system used by humans. In a real circuit or installation, mastering this translation dictates how you physically configure hardware addresses (like I2C expanders or DMX lighting fixtures), interpret microcontroller GPIO register values, and map raw analog-to-digital converter (ADC) outputs to real-world voltages. If you are setting up a PCF8574 I/O expander or flipping DIP switches on a motor driver, you are doing this math on the bench. The direct answer to 'how do I convert' is to use positional weighting: every bit in a binary string represents a specific power of 2, and summing those powers gives you the decimal equivalent.
The Core Translation Tables
Before doing long division, memorize the positional weights of an 8-bit byte. Digital logic relies heavily on 8-bit, 16-bit, and 32-bit boundaries. The table below maps the physical bit index to its decimal weight, hexadecimal equivalent, and a common hardware use case. Keep this table in your head when debugging shift registers like the 74HC595 or configuring port masks on an ATmega328P.
| Bit Index | Weight (Decimal) | Hex Value | Binary Mask | Common Hardware Use Case |
|---|---|---|---|---|
| 7 (MSB) | 128 | 0x80 | 10000000 | Sign bit (signed integers) / Enable flag |
| 6 | 64 | 0x40 | 01000000 | ADC high-bit / Interrupt mask |
| 5 | 32 | 0x20 | 00100000 | SPI MISO line / Direction control |
| 4 | 16 | 0x10 | 00010000 | I2C address bit / Chip select |
| 3 | 8 | 0x08 | 00001000 | UART TX/RX configuration |
| 2 | 4 | 0x04 | 00000100 | Timer prescaler bit 2 |
| 1 | 2 | 0x02 | 00000010 | Timer prescaler bit 1 |
| 0 (LSB) | 1 | 0x01 | 00000001 | Parity bit / Lowest GPIO pin |
According to the All About Circuits digital textbook, understanding these positional weights is the foundation of all microprocessor architecture. When you write PORTD = 0x80 in C++, the compiler uses this exact table to pull physical pin PD7 high while leaving PD0 through PD6 low.
Worked Numeric Examples: Decimal to Binary and Back
Let's run through two concrete examples using the subtraction method, which is generally faster on the bench than the division-by-2 method taught in introductory computer science classes.
Example 1: Decimal 173 to Binary
You need to send the decimal value 173 to an 8-bit digital-to-analog converter (DAC). What is the binary string?
- 128: 173 is greater than 128. Bit 7 = 1. (Remainder: 173 - 128 = 45)
- 64: 45 is less than 64. Bit 6 = 0.
- 32: 45 is greater than 32. Bit 5 = 1. (Remainder: 45 - 32 = 13)
- 16: 13 is less than 16. Bit 4 = 0.
- 8: 13 is greater than 8. Bit 3 = 1. (Remainder: 13 - 8 = 5)
- 4: 5 is greater than 4. Bit 2 = 1. (Remainder: 5 - 4 = 1)
- 2: 1 is less than 2. Bit 1 = 0.
- 1: 1 is equal to 1. Bit 0 = 1. (Remainder: 0)
Reading from Bit 7 down to Bit 0, the binary string is 10101101.
Example 2: Binary 11001010 to Decimal
You read a status register from a sensor via I2C and the logic analyzer shows 11001010. What decimal error code is this?
- Bit 7 (1) = 128
- Bit 6 (1) = 64
- Bit 5 (0) = 0
- Bit 4 (0) = 0
- Bit 3 (1) = 8
- Bit 2 (0) = 0
- Bit 1 (1) = 2
- Bit 0 (0) = 0
Summing the active weights: 128 + 64 + 8 + 2 = 202. You can then cross-reference 202 in the sensor's datasheet.
Where You Meet This in Practice
Theory is fine, but binary conversion is a physical, hands-on task in electrical and electronics work. Here are three scenarios where you will manually convert binary to decimal and decimal to binary on the jobsite or at the workbench.
1. DMX512 Lighting and Motor Control DIP Switches
In theatrical lighting and industrial motor control, DMX512 is the standard protocol. A single DMX universe has 512 channels. To assign a moving head light to channel 137, you must flip the physical 9-position or 10-position DIP switches on the fixture's back panel. Channel 137 in decimal requires you to find the binary weights: 128 + 8 + 1 = 137. Therefore, you flip the switches representing 128, 8, and 1 to the 'ON' position. If you miscalculate this binary-to-decimal conversion, your light will either go dark or respond to the wrong control desk fader.
2. I2C Address Configuration
When wiring multiple identical sensors (like BME280 environmental sensors) to an ESP32 via I2C, you must change their hardware addresses to avoid bus collisions. The base I2C address might be 0x76 (decimal 118, binary 01110110). If the breakout board has an A0 jumper pad that adds '1' to the least significant bit when bridged, the new binary address becomes 01110111, which is 0x77 in hex and 119 in decimal. You must convert this back to decimal or hex to pass the correct integer into your Wire.beginTransmission() function.
3. Direct GPIO Port Manipulation
When toggling pins using digitalWrite() is too slow for high-frequency PWM or bit-banging, you write directly to the microcontroller's port registers. As detailed in the official Arduino Port Manipulation reference, setting PORTB = B00100100 instantly sets physical pins PB2 and PB5 high. If you need to turn on PB3 as well without disturbing the others, you must mentally convert B00100100 to decimal (36), add the weight of PB3 (8), and write PORTB = 44, or simply use bitwise OR operations (PORTB |= (1 << 3)).
Common Pitfalls and What People Confuse
When converting between number systems for hardware, three specific confusions lead to bricked buses, short circuits, or erratic behavior.
Endianness: MSB vs. LSB Physical Layout
The most common mistake is assuming the physical switch labeled '1' on a DIP bank always represents the decimal weight of 1 (the Least Significant Bit, or LSB). On many DMX fixtures and legacy PLC modules, the switch labeled '1' actually represents the Most Significant Bit (MSB, weight 128 or 256). Always check the manufacturer's silkscreen. If you treat an MSB-first switch bank as LSB-first, your decimal 137 will be interpreted by the machine as a completely different address, often exceeding the valid range and causing the device to default to address 0.
Zero-Indexing vs. One-Indexing
Humans count starting at 1. Digital logic counts starting at 0. An 8-bit byte does not have bits 1 through 8; it has bits 0 through 7. When a datasheet says 'set bit 4 high', it means the 5th physical pin from the right (weight 16), not the 4th pin (weight 8). Confusing the bit index with the bit count is the primary reason beginners fail to configure timer prescalers correctly on AVR and ARM Cortex-M microcontrollers.
Signed vs. Unsigned Integers (Two's Complement)
Binary 11111111 is decimal 255 if the system is reading an unsigned 8-bit integer (like a raw ADC value or a PWM duty cycle). However, if the microcontroller is reading a signed 8-bit integer (like temperature data from an accelerometer), 11111111 represents decimal -1 using Two's Complement notation. Always verify whether the sensor datasheet specifies signed or unsigned data formats before converting the binary register dump to a decimal engineering unit.
Frequently Asked Questions
Why do we use hexadecimal instead of just binary or decimal?
Hexadecimal (base-16) acts as a human-readable shorthand for binary. Because 16 is a power of 2 (2^4), exactly four binary bits map to one single hex character. The binary string 10101101 is cumbersome to read, but its hex equivalent 0xAD is instantly recognizable to an embedded engineer. It bridges the gap between human decimal logic and machine binary execution.
How do I convert a decimal fraction (like 0.625) to binary?
For fractional decimal-to-binary conversion, you repeatedly multiply the fractional part by 2. If the result is 1 or greater, the binary bit is 1, and you subtract 1 before the next multiplication. For 0.625: (0.625 x 2 = 1.25 -> bit 1), (0.25 x 2 = 0.5 -> bit 0), (0.5 x 2 = 1.0 -> bit 1). The binary fraction is 0.101. This is heavily used in floating-point math and DSP (Digital Signal Processing) filter coefficients.
What is the maximum decimal value for a 10-bit ADC?
A 10-bit Analog-to-Digital Converter, like the one built into the classic Arduino Uno's ATmega328P, has 2^10 possible states. Because it counts from 0, the maximum decimal value is 1023. When mapping this to a 5V reference, each binary step represents approximately 4.88 millivolts (5.0 / 1024).
For further reading on logic levels and how binary states translate to physical voltages on a breadboard, review the SparkFun guide to Logic Levels. Mastering these conversions ensures you spend less time debugging address conflicts and more time building functional circuits.






