In digital electronics and computing, binary 40 is the base-2 representation of the decimal number 40, written as 101000 (or 00101000 when padded to a standard 8-bit byte). Writing this exact value to a microcontroller's GPIO register changes the physical voltage state of specific pins—specifically pulling pins 3 and 5 HIGH on an ATmega328P—allowing you to drive multiple MOSFET gates or LEDs simultaneously without relying on slow software loops. However, makers frequently confuse the decimal integer 40 with the literal binary string 101000 in code, accidentally writing PORTD = 101000; (which assigns decimal one-hundred-and-one-thousand) instead of using the proper 0b prefix or hexadecimal 0x28.
The Anatomy of Binary 40 in an 8-Bit Register
To understand how decimal 40 translates to physical hardware states, we must break it down into its binary weights. In an 8-bit system, the value 40 is achieved by summing the weights of the 5th bit (32) and the 3rd bit (8). 32 + 8 = 40.
When you apply this to a real microcontroller, such as the ATmega328P found on the Arduino Uno, those specific bits map directly to physical silicon pins. The table below maps the 8-bit binary representation of 40 to the exact PORTD register pins and their corresponding physical headers on the board.
| Bit Position | Weight | Binary State | ATmega328P Register | Arduino Uno Physical Pin |
|---|---|---|---|---|
| Bit 7 (MSB) | 128 | 0 | PD7 | D7 |
| Bit 6 | 64 | 0 | PD6 | D6 |
| Bit 5 | 32 | 1 | PD5 | D5 (PWM) |
| Bit 4 | 16 | 0 | PD4 | D4 |
| Bit 3 | 8 | 1 | PD3 | D3 (PWM) |
| Bit 2 | 4 | 0 | PD2 | D2 |
| Bit 1 | 2 | 0 | PD1 | D1 (TX) |
| Bit 0 (LSB) | 1 | 0 | PD0 | D0 (RX) |
0x28. In octal, it is 050. When reading Texas Instruments shift register datasheets, you will almost always see register states referenced in hex rather than raw binary strings.
Where You Meet Binary 40 in Practice
Understanding this specific bit pattern is not just an academic exercise; it dictates how you interface with hardware at the lowest software level. You will encounter the need to output binary 40 in three primary scenarios:
1. Direct Port Manipulation (Bitmasking)
Using digitalWrite() in a loop is slow. If you need to toggle multiple pins simultaneously—such as driving a stepper motor coil sequence or latching data into an external ADC—you write directly to the PORT register. Outputting 40 to PORTD instantly sets Pins 3 and 5 HIGH in a single clock cycle, eliminating the microsecond delay between sequential digitalWrite() calls.
2. Shift Register Payloads
When daisy-chaining 74HC595 shift registers to expand your I/O, you send byte-sized payloads over SPI or bit-banged GPIO. Sending 0x28 (decimal 40) to a single 74HC595 will turn on outputs QD (Pin 7) and QF (Pin 5) on the IC, leaving the other six outputs LOW. This is heavily used in LED matrix multiplexing and relay bank control.
3. 4000-Series CMOS Counters
If you are using a CD4040 12-stage binary ripple counter, the IC increments its internal flip-flops on every falling edge of the clock pin. When the counter reaches a decimal count of 40, the physical output pins Q3 and Q5 will be HIGH, while the rest remain LOW. Recognizing this hardware state allows you to build an analog decoder circuit using a simple 2-input AND gate to trigger a reset or an alarm exactly at the 40th pulse.
Worked Numeric Example: Sizing Current for a 40-State Bitmask
Let’s look at a real-world bench scenario. You are using an Arduino Uno (ATmega328P) and you write binary 40 (0b00101000) to PORTD. Pins 3 and 5 are now sourcing 5V. You have connected an LED with a forward voltage ($V_f$) of 2.1V to each pin, using a 220Ω current-limiting resistor.
Step 1: Calculate current per pin
Using Ohm's Law: $I = (V_{cc} - V_f) / R$
$I = (5.0V - 2.1V) / 220\Omega$
$I = 2.9V / 220\Omega = 0.0131A$ (or 13.1 mA per pin)
Step 2: Calculate total PORTD sourcing current
Since only two pins are HIGH in the binary 40 state, the total current sourced by the PORTD VCC pin is:
$13.1 mA \times 2 = 26.2 mA$
Step 3: Verify against silicon limits
The ATmega328P datasheet specifies a maximum DC current per I/O pin of 40 mA (absolute maximum, 20 mA recommended), and a maximum total current for all PORTD pins combined of 100 mA. At 26.2 mA total, your binary 40 configuration is operating safely within the digital logic thresholds and thermal limits of the microcontroller.
Safe Implementation: Copy-Paste Code
A common mistake when manipulating ports is overwriting the entire register, which can accidentally disable the hardware serial pins (RX/TX on Pins 0 and 1) or disrupt interrupts. The code below uses bitwise OR (|=) and bitwise AND-NOT (&= ~) to safely apply and remove the binary 40 mask without touching the other bits.
// Safe direct port manipulation for Decimal 40 (Pins 3 and 5)
void setup() {
// Configure only pins 3 and 5 as outputs without touching RX/TX (Pins 0/1)
// 0b00101000 is decimal 40
DDRD |= 0b00101000;
}
void loop() {
// Turn ON pins 3 and 5, preserving other PORTD states
PORTD |= 0b00101000;
delay(500);
// Turn OFF pins 3 and 5, preserving other PORTD states
PORTD &= ~0b00101000;
delay(500);
}
Common Syntax Traps and How to Avoid Them
If you type
PORTD = 101000; in your C++ code, the compiler reads this as a base-10 integer (one hundred and one thousand). Because an 8-bit register can only hold values up to 255, this will overflow and truncate, resulting in unpredictable pin states. Always use the 0b prefix for binary (0b00101000), 0x for hex (0x28), or just use the decimal integer (40).
FAQ: Clearing Up Binary Confusions
Is "binary 40" the same as the CD4040 chip?
No. The CD4040 is a specific 16-pin CMOS integrated circuit (a 12-stage ripple counter) from the 4000-series logic family. "Binary 40" refers strictly to the mathematical base-2 representation of the number 40. However, when a CD4040 chip counts up to the decimal number 40, its output pins will physically reflect the binary 40 state.
Why do we pad binary 40 to 8 bits (00101000) instead of just writing 101000?
Microcontroller registers and shift registers are hardware-bound to specific widths, usually 8, 16, or 32 bits. Padding with leading zeros ensures you are visually accounting for every physical pin in the register, preventing off-by-one errors when mapping bits to physical breadboard wires.
What happens if I send binary 40 to an ESP32 instead of an Arduino Uno?
The ESP32 does not use the simple PORTD architecture of the ATmega328P. Instead, it uses 32-bit GPIO registers (like GPIO_OUT_W1TS_REG). To set GPIO 3 and GPIO 5 HIGH on an ESP32 using direct register manipulation, you would write a 32-bit mask: 1 << 3 | 1 << 5, which still equals decimal 40, but the underlying C-macro and register names are entirely different.






