The binary for 20 is 10100, a base-2 numerical representation where the 16-bit and 4-bit positional weights are high (1) while the 8, 2, and 1 positions are low (0).
In a physical digital circuit, sending this binary sequence to a microcontroller port or shift register forces exactly the 3rd and 5th output pins (zero-indexed) to go HIGH (VCC), energizing the specific relays, LEDs, or logic gates attached to those traces while keeping the rest LOW (GND). Understanding how to map this decimal value to physical hardware states is a foundational skill for embedded systems and digital logic design.
The Direct Answer: Binary for 20 and How It Works
Digital logic relies on powers of two. To convert decimal 20 to binary, we subtract the largest power of 2 that fits into our number, mark that bit as a 1, and repeat the process with the remainder.
- 16 fits into 20. (Remainder: 4) → Bit 4 is
1 - 8 does not fit into 4. → Bit 3 is
0 - 4 fits into 4. (Remainder: 0) → Bit 2 is
1 - 2 does not fit into 0. → Bit 1 is
0 - 1 does not fit into 0. → Bit 0 is
0
Reading from highest bit to lowest, we get 10100. In microcontroller programming, we rarely use 5-bit variables; we pad this to a standard 8-bit byte: Decimal 20 = Binary 00010100 = Hex 0x14. When a microcontroller pushes this byte to an output register, the physical voltage on pins 2 and 4 rises to the logic HIGH threshold (typically 3.3V or 5V, depending on your board's VCC), while pins 0, 1, 3, 5, 6, and 7 remain at 0V.
Worked Example: Driving a 74HC595 Shift Register with Binary 20
Let's look at a real-world bench scenario. You are building an automated irrigation controller using an ESP32-WROOM-32, but you've run out of GPIO pins. You decide to use a Texas Instruments SN74HC595 8-bit shift register to control eight 5V solenoid valves via a ULN2803A Darlington transistor array.
You need to open Valve 3 (connected to shift register output Q2) and Valve 5 (connected to output Q4) simultaneously, leaving all others closed. Here is the exact pin-to-weight mapping for the 74HC595:
| Shift Register Pin | Output Name | Binary Weight | State for Decimal 20 |
|---|---|---|---|
| 15 | Q0 | 1 | 0 (LOW) |
| 1 | Q1 | 2 | 0 (LOW) |
| 2 | Q2 | 4 | 1 (HIGH) |
| 3 | Q3 | 8 | 0 (LOW) |
| 4 | Q4 | 16 | 1 (HIGH) |
| 5 | Q5 | 32 | 0 (LOW) |
| 6 | Q6 | 64 | 0 (LOW) |
| 7 | Q7 | 128 | 0 (LOW) |
To achieve this, we send the binary for 20 (00010100) to the shift register. Here is the exact wiring and C++ code to execute this on an ESP32:
// ESP32 Shift Register Control for Binary 20
const int dataPin = 13; // SER (Pin 14 on 74HC595)
const int latchPin = 12; // RCLK (Pin 12 on 74HC595)
const int clockPin = 14; // SRCLK (Pin 11 on 74HC595)
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
// Open Valves 3 and 5 (Decimal 20)
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0b00010100); // Binary for 20
digitalWrite(latchPin, HIGH);
delay(5000); // Keep valves open for 5 seconds
// Close all valves (Decimal 0)
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0b00000000);
digitalWrite(latchPin, HIGH);
delay(10000); // Wait 10 seconds before next cycle
}
In this code, 0b00010100 tells the compiler to interpret the literal as binary. The shiftOut function pulses the clock pin 8 times, pushing the 1s into the Q2 and Q4 positions, which then drive the base of the Darlington pairs, pulling the solenoid gates to ground and opening the valves.
Where You Meet Binary 20 in Practice
Beyond shift registers, you will encounter the binary representation of 20 in several specific hardware configuration scenarios:
- DMX512 Lighting Addresses: In stage lighting, fixtures are assigned a starting address using a bank of 9 or 10 physical DIP switches. To set a moving head spotlight to DMX address 20, you flip switches 5 (value 16) and 3 (value 4) to the ON position. The internal microcontroller reads this physical binary
10100on boot. - Stepper Motor Microstepping: Many TB6600 or DRV8825 stepper motor drivers use a 3-switch or 6-switch block to set current limits and microstep resolution. While 20 isn't a direct microstep divisor, configuring a custom current decay table via an I2C digital potentiometer often requires writing specific binary bytes to the wiper register.
- Parallel EEPROM Addressing: When writing data to a legacy 28C64 parallel EEPROM, the address bus (A0-A12) must be set to write to specific memory locations. To write to memory address 20, the microcontroller must drive address lines A4 and A2 HIGH, matching the
10100binary pattern.
Common Confusions: Binary 20 vs. Hexadecimal and BCD
People commonly confuse binary 20 with hexadecimal 20 and Binary-Coded Decimal (BCD) 20. This confusion leads to off-by-magnitude errors that can brick configurations or trigger the wrong hardware outputs.
| Format | Notation | Actual Decimal Value | 8-Bit Binary Equivalent |
|---|---|---|---|
| Binary 20 | 0b00010100 | 20 | 00010100 |
| Hexadecimal 20 | 0x20 | 32 | 00100000 |
| BCD 20 | 0010 0000 | 20 (encoded as two nibbles) | 00100000 |
If you are reading a datasheet for an I2C sensor and it says 'set the configuration register to 20', it almost always means decimal 20 (0x14 in hex). If you accidentally type 0x20 in your Arduino code, you are sending decimal 32, which will configure the sensor incorrectly. Always verify whether the documentation implies decimal, hex, or raw binary.
Frequently Asked Questions About Binary for 20
How do I write the binary for 20 in Arduino or ESP32 C++ code?
In modern C++ (and the Arduino framework), you prefix the binary number with 0b. Therefore, you write it as 0b00010100 or simply 0b10100. Older Arduino IDE versions also supported the B00010100 macro, but 0b is the standard C++14 compliant method and is guaranteed to work across all GCC and Clang compilers used in embedded development.
What is the 8-bit padded binary format for 20?
The 8-bit padded format is 00010100. Microcontrollers process data in 8-bit (byte), 16-bit (int), or 32-bit (long) chunks. While the mathematical binary for 20 is just five digits (10100), adding the leading zeros is critical when performing bitwise operations like masking or bit-shifting, as it ensures the compiler allocates the correct memory width and prevents sign-extension errors if the variable is cast to a signed integer.
Which physical DIP switches do I flip to set a binary value of 20?
If you are setting a hardware address on a DMX decoder or a binary-coded rotary switch, you flip the switches corresponding to the 1 bits in the sequence. For 20 (10100), you flip the switch labeled '16' (often physically the 5th switch) and the switch labeled '4' (the 3rd switch). All other switches (1, 2, 8, 32, etc.) must remain in the OFF or '0' position.
Why does my shift register output the wrong pins when I send binary 20?
This is almost always caused by a mismatch in bit-ordering during the shiftOut() function. If you use MSBFIRST (Most Significant Bit First), the leftmost bit in your byte enters the shift register first and ends up at Q7. If you use LSBFIRST, the rightmost bit enters first and ends up at Q7. If your physical wiring assumes MSBFIRST but your code sends LSBFIRST, the 1s meant for Q2 and Q4 will end up mirrored at Q5 and Q3. Always verify your bit-order against the shift register wiring tutorial for your specific breakout board.






