Binary is a base-2 numeral system that represents numeric values using only two symbols, typically 0 and 1, where each position corresponds to an increasing power of two. If you are programming an ESP32, setting PLC addresses, or reading a datasheet register, knowing how to find the binary of a number is the bridge between human-readable decimal math and the physical HIGH/LOW states of your hardware pins. While calculators can do this instantly, understanding the manual conversion process is critical for debugging shift registers, configuring subnet masks, and writing efficient embedded C code.
The Core Math: The Subtraction Method for Hardware
There are two standard ways to convert a decimal number to binary: the division-by-2 method (which is great for writing software algorithms) and the subtraction-of-powers method. For bench work and embedded systems, the subtraction method is vastly superior because it maps directly to physical bit positions in a byte or word.
Let's look at a worked numeric example. Suppose you need to convert the decimal number 173 into an 8-bit binary byte. We start by listing the powers of 2 for an 8-bit system, from the Most Significant Bit (MSB) to the Least Significant Bit (LSB): 128, 64, 32, 16, 8, 4, 2, 1.
| Bit Position | 7 (MSB) | 6 | 5 | 4 | 3 | 2 | 1 | 0 (LSB) |
|---|---|---|---|---|---|---|---|---|
| Decimal Weight | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Can we subtract? | Yes (173-128=45) | No (45 < 64) | Yes (45-32=13) | No (13 < 16) | Yes (13-8=5) | Yes (5-4=1) | No (1 < 2) | Yes (1-1=0) |
| Resulting Bit | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
10101101 = Hexadecimal 0xAD
By working left to right, you ask a simple question at each step: Is my remaining value greater than or equal to this bit's weight? If yes, the bit is a 1, and you subtract that weight from your running total. If no, the bit is a 0, and you carry the remainder to the next column. This exact logical sequence is what a successive approximation register (SAR) ADC uses internally to digitize analog voltages.
Where You Meet Binary in Real Circuits and Installations
Understanding binary conversion changes how you interact with physical hardware. It dictates physical switch positions, memory mapping, and communication protocols.
1. Hardware Addressing via DIP Switches
When installing a DMX512 lighting fixture or configuring the Modbus RS-485 address on a Variable Frequency Drive (VFD), you will often encounter a bank of 8 or 10 physical DIP switches. The manual might instruct you to 'Set the device address to 173'. You cannot type '173' into a physical switch; you must flip the switches to match the binary equivalent. If the switches are labeled 1 through 8 (representing weights 1 through 128), you will flip switches 1, 3, 4, 6, and 8 to the ON position. Misunderstanding whether the manufacturer labels the switches starting from the LSB or the MSB is a leading cause of commissioning failures in industrial controls.
2. Direct Port Manipulation in Microcontrollers
On an ATmega328P (the chip inside the Arduino Uno), calling digitalWrite() eight times to set a byte of data is slow and consumes valuable CPU cycles. Instead, engineers write directly to the PORT register. If you want to set pins 7, 5, 3, 2, and 0 HIGH on PORTD, you write the binary number you just calculated:
PORTD = 0b10101101;
According to the Arduino port manipulation documentation, this executes in a single clock cycle. However, you must be careful: writing directly to a port bypasses the Arduino core's safety checks, meaning you can accidentally overwrite the state of the hardware UART pins (D0 and D1) if you aren't tracking your bit positions correctly.
3. I2C and SPI Addressing
In I2C communication, devices have a 7-bit address. However, the protocol sends an 8-bit byte where the 8th bit is the Read/Write flag. If a sensor's datasheet lists its 7-bit address as 0x68 (decimal 104, binary 1101000), the microcontroller must shift that binary sequence left by one bit to make room for the R/W bit, resulting in 11010000 (0xD0) for a write operation. As detailed in the NXP I2C-bus specification, failing to account for this binary shift is the most common reason hobbyists get 'NACK' errors when scanning the I2C bus.
Common Confusions: What Binary is Not
When learning how to find the binary of a number, beginners frequently confuse the underlying base-2 reality with the shorthand notations we use to make it readable.
People often confuse binary and hexadecimal, treating them as competing systems. Hex (base-16) is simply a human-friendly compression of binary. Because 16 is a power of 2 ($2^4$), exactly four binary bits map to one hex digit. The silicon in your ESP32 does not 'think' in hex; it only sees HIGH and LOW voltages. Hex is just how we write binary without getting a headache. For a deeper dive into this relationship, see SparkFun's binary tutorial.
Endianness (MSB vs LSB): Binary defines the value, but endianness defines the transmission order. When shifting bits into a 74HC595 shift register over SPI, the datasheet will specify 'MSB first' or 'LSB first'. If your binary number is 10101101, 'MSB first' means the 128-weight bit hits the wire first. 'LSB first' means the 1-weight bit hits first. The decimal value is the same, but the physical wiring of your shift register outputs will be completely reversed if you guess wrong.
Signed vs. Unsigned (Two's Complement): The binary sequence 10101101 equals 173 if the variable is an unsigned 8-bit integer. However, if the microcontroller treats it as a signed 8-bit integer, that exact same binary sequence represents -83. The MSB acts as a negative sign indicator in signed math, which drastically changes how you interpret sensor data returning from an accelerometer.
FAQ: Finding and Using Binary Numbers
How to find the binary of a negative number in a microcontroller?
Microcontrollers use a system called Two's Complement to represent negative numbers. To find the binary of a negative decimal (e.g., -83 in an 8-bit system), first find the binary of the positive equivalent (83 = 01010011). Next, invert every bit (change 0s to 1s and 1s to 0s), giving 10101100. Finally, add 1 to the result. 10101100 + 1 = 10101101. This is why an 8-bit signed integer maxes out at +127 instead of +255; the top half of the binary space is reserved for negative values.
How to find the binary of a fractional number?
To convert a decimal fraction (like 0.625) to binary, you use negative powers of 2 (0.5, 0.25, 0.125, 0.0625) and the subtraction method. For 0.625: it contains one 0.5 (remainder 0.125). It contains zero 0.25s. It contains one 0.125 (remainder 0). The binary representation is 0.101. This exact math is used when configuring the fractional dividers in hardware PLLs (Phase-Locked Loops) for clock generation.
What is the fastest way to find the binary of a number without a calculator?
Memorize the first eight powers of two: 128, 64, 32, 16, 8, 4, 2, 1. Once you have this sequence locked in your memory, you can do the subtraction method in your head in about three seconds for any number up to 255. For 16-bit numbers, extend the sequence to 32768, 16384, 8192, etc. On the bench, writing these eight numbers across the top of your whiteboard or notebook will save you from reaching for your phone every time you need to set a DIP switch.
How do I convert a binary string back to decimal in my head?
Read the binary string from right to left. Every time you see a '1', add the corresponding power of 2 to your mental total. For example, looking at 10110: the rightmost bit is 0 (skip), the next is 1 (add 2), the next is 1 (add 4), the next is 0 (skip), and the leftmost is 1 (add 16). Total: 16 + 4 + 2 = 22. This reverse-lookup skill is essential when reading logic analyzer traces or oscilloscope captures of serial data lines.






