Binary is a base-2 numbering system that represents any decimal value using only two states: 1 (on/high) and 0 (off/low). In a real circuit, mastering this translation dictates how you map physical pin voltages (0V or 3.3V) to logical data, configure hardware addresses on I2C port expanders, and calculate exact PWM duty cycles on microcontrollers like the ESP32. Beginners commonly confuse binary math (the base-2 numerical value) with binary logic families (the physical voltage thresholds, like 5V TTL vs. 3.3V CMOS), or they mix up base-2 with the base-16 hexadecimal shorthand used in memory datasheets.
1 for every power you successfully subtract, and a 0 for every power you skip.
The Subtraction Method: A Worked Numeric Example
While repeated division by 2 is the standard classroom method for finding the binary value of a number, the subtraction method (also called the powers-of-2 method) is vastly superior for electronics work. When you are configuring an 8-bit hardware register, you already know your boundary is 255, making it faster to subtract from left to right.
Let’s convert the decimal number 173 into an 8-bit binary value. This is a common value you might write to a PWM register to set a specific motor speed or LED brightness.
We start with the highest power of 2 that fits in an 8-bit register (27 = 128) and work our way down to 20 (1).
| Power of 2 | Value | Can we subtract it from the remainder? | Binary Bit | New Remainder |
|---|---|---|---|---|
| 27 | 128 | Yes (173 - 128) | 1 | 45 |
| 26 | 64 | No (64 is larger than 45) | 0 | 45 |
| 25 | 32 | Yes (45 - 32) | 1 | 13 |
| 24 | 16 | No (16 is larger than 13) | 0 | 13 |
| 23 | 8 | Yes (13 - 8) | 1 | 5 |
| 22 | 4 | Yes (5 - 4) | 1 | 1 |
| 21 | 2 | No (2 is larger than 1) | 0 | 1 |
| 20 | 1 | Yes (1 - 1) | 1 | 0 |
Reading the binary bits from left (Most Significant Bit) to right (Least Significant Bit), we get 10101101. In Arduino or ESP32 C++ code, you would write this as 0b10101101 or its hexadecimal equivalent, 0xAD. According to the Arduino byte data type reference, a byte stores an 8-bit unsigned number, making this exact 8-bit sequence the native language of microcontroller memory.
Where You Meet Binary in Physical Circuits
Understanding how to find the binary value of a number isn’t just a math exercise; it directly changes how you wire and program physical hardware. Here is where base-2 math dictates physical circuit behavior:
1. I2C Address Configuration via DIP Switches
When you wire an I2C port expander like the MCP23017 or a PCF8574, the chip has address pins (usually A0, A1, A2). The base I2C address is 0x20. If you wire A2 to VCC (HIGH/1), A1 to GND (LOW/0), and A0 to VCC (HIGH/1), you are physically building the binary number 101 (decimal 5). The chip’s internal logic adds this binary value to the base address, resulting in a final I2C address of 0x25. If you cannot translate physical switch states to binary, you will never find your chip on the I2C bus.
2. Bit-Masking for GPIO Port Manipulation
Suppose you have an 8-bit output port controlling eight relays, and the current state is 10101101 (relays 1, 3, 4, 6, and 8 are energized). You need to turn off relay 4 (the 3rd bit from the right) without disturbing the others. You use a binary AND mask. By converting your target state to binary, you can apply a bitmask (11111011) to safely clear that single bit without accidentally dropping power to the other relays. For a deeper look at how these logic gates process binary states physically, the All About Circuits digital electronics textbook provides excellent schematics of the underlying transistor logic.
3. PWM Duty Cycle Registers
On an ESP32, the LEDC (LED Control) peripheral uses hardware registers to set PWM duty cycles. If you configure a 10-bit resolution, the register accepts values from 0 to 1023. If you want exactly a 50% duty cycle, you must calculate half of 1024 (512). To understand what the hardware is actually doing, you translate 512 to binary: 0b1000000000. The microcontroller literally sets the 9th bit high and all others low inside the timer comparator register.
Binary vs. Hexadecimal: Why Makers Use Both
While you must know how to find the binary value of a number to understand the hardware, you will rarely type raw binary into your code. Makers use hexadecimal (base-16) as a human-readable bridge. Here is how the two formats divide the workload in a typical embedded project:
| Criteria | Binary (Base-2) | Hexadecimal (Base-16) |
|---|---|---|
| Primary Use Case | Bit-masking, pin states, logic analysis | Memory addresses, color codes, I2C/SPI registers |
| Visual Length | Long (e.g., 11111010) |
Short (e.g., 0xFA) |
| Hardware Mapping | 1-to-1 mapping with physical pins/wires | 1-to-1 mapping with memory bytes |
| C++ Prefix | 0b (e.g., 0b1010) |
0x (e.g., 0x0A) |
| When to Choose | When manipulating individual bits (AND/OR/XOR) | When passing full byte values to library functions |
The Golden Rule: Use binary when you care about individual pins. Use hexadecimal when you care about the whole byte.
Frequently Asked Questions
How to find the binary value of a negative number?
Microcontrollers represent negative numbers using a system called Two’s Complement. To find the binary value of a negative decimal (e.g., -5 in an 8-bit system):
1. Find the binary value of the positive number (5 = 00000101).
2. Invert all the bits (flip 1s to 0s and 0s to 1s), resulting in 11111010.
3. Add 1 to the result: 11111010 + 00000001 = 11111011.
Therefore, -5 in 8-bit binary is 11111011 (or 0xFB in hex). The Most Significant Bit (MSB) acting as a 1 is the universal hardware flag that a number is negative.
How to find the binary value of a decimal fraction?
To convert a fractional decimal like 0.625 to binary, you use negative powers of 2 (0.5, 0.25, 0.125, 0.0625) and subtract them from your target, exactly like the integer method.
• 0.625 - 0.5 (2-1) = 0.125 (Bit = 1)
• 0.125 - 0.25 (2-2) = Cannot subtract (Bit = 0)
• 0.125 - 0.125 (2-3) = 0 (Bit = 1)
The binary fraction is 0.101. Note that many common decimal fractions (like 0.1) result in infinitely repeating binary fractions, which is why floating-point math on an Arduino Uno can sometimes yield slight rounding errors in the least significant digits.
How to find the binary value of a number using division?
If you prefer the division method, divide your decimal number by 2. Record the remainder (which will always be 0 or 1). Take the quotient and divide it by 2 again. Repeat this until the quotient reaches 0. The binary value is found by reading your recorded remainders backwards (from the last remainder obtained to the first). For 173: 173/2 = 86 R(1), 86/2 = 43 R(0), 43/2 = 21 R(1), 21/2 = 10 R(1), 10/2 = 5 R(0), 5/2 = 2 R(1), 2/2 = 1 R(0), 1/2 = 0 R(1). Reading bottom-to-top gives 10101101. While mathematically sound, this method is slower for mental math on the workbench than the subtraction method.






