The Direct Answer: 6 in Binary Code and Bitwise Basics
The decimal number 6 is written as 0110 in 4-bit binary code, and 00000110 in standard 8-bit binary. In hexadecimal, it is represented as 0x06.
To understand why, look at the base-2 positional weight of each bit. In a 4-bit system, the columns from right to left represent $2^0$ (1), $2^1$ (2), $2^2$ (4), and $2^3$ (8). To get 6, you need a 4 and a 2. Therefore, the '4' column gets a 1, the '2' column gets a 1, and the '8' and '1' columns get a 0. Result: 0110.
0b. Writing 0b0110 tells the compiler to interpret the digits as base-2. If you just type 0110, the compiler assumes it is an octal (base-8) literal, which equals decimal 72—a classic bug that causes erratic GPIO behavior.
In embedded systems, manipulating binary code is how we control hardware registers, set GPIO pin states, and configure I2C/SPI peripherals. To prove this, we are going to build a 4-bit binary visualizer that outputs the number 6 (and cycles through other values) using physical LEDs on an ESP32.
Project Build: 4-Bit Binary Visualizer Hardware
This project maps the four bits of our binary number to four physical GPIO pins. We are specifically targeting the ESP32-WROOM-32E DevKit V1 (the standard 38-pin or 30-pin USB-C board widely available in 2026).
Parts List
- Microcontroller: ESP32-WROOM-32E DevKit V1 (Espressif)
- LEDs: 4x 5mm Red Diffused LEDs (2.0V forward voltage, 20mA max)
- Resistors: 4x 330Ω 1/4W carbon film resistors (Red-Orange-Brown-Gold)
- Prototyping: 400-point solderless breadboard, male-to-male jumper wires
- Power: USB-C data cable connected to a 5V/1A+ power brick
Pin Mapping Table
A critical mistake beginners make is assigning binary bits to sequential pins on the ESP32 silkscreen. Pins GPIO 34, 35, 36, and 39 are input-only on the WROOM-32E. If you try to drive them HIGH, the ESP-IDF background task will throw a panic error. We use safe, output-capable GPIOs on the left rail.
| Binary Bit | Positional Weight | ESP32 GPIO | Physical Component |
|---|---|---|---|
| Bit 0 (LSB) | 1 ($2^0$) | GPIO 25 | LED 1 + 330Ω Resistor |
| Bit 1 | 2 ($2^1$) | GPIO 26 | LED 2 + 330Ω Resistor |
| Bit 2 | 4 ($2^2$) | GPIO 27 | LED 3 + 330Ω Resistor |
| Bit 3 (MSB) | 8 ($2^3$) | GPIO 14 | LED 4 + 330Ω Resistor |
Wiring note: Connect the 330Ω resistor to the GPIO pin, then to the LED anode (long leg). Connect the LED cathode (short leg) to the breadboard ground rail, which ties back to the ESP32 GND pin.
Complete ESP32 Code with GPIO Error Handling
The following C++ code is written for the Arduino IDE (ESP32 board package v3.x). It uses bitwise shift (>>) and bitwise AND (&) operators to extract each bit of the number 6 and write it to the corresponding LED.
// Target Board: ESP32-WROOM-32E DevKit V1
// Framework: Arduino IDE (ESP32 Core v3.x)
const int LED_PINS[4] = {25, 26, 27, 14}; // LSB to MSB
const int PIN_COUNT = 4;
void setup() {
Serial.begin(115200);
delay(500); // Allow serial monitor to connect
Serial.println("Booting 4-Bit Binary Visualizer...");
// Initialize GPIO pins as outputs
for (int i = 0; i < PIN_COUNT; i++) {
pinMode(LED_PINS[i], OUTPUT);
digitalWrite(LED_PINS[i], LOW); // Start with all LEDs off
}
}
void displayBinary(byte value) {
// Safety mask: ensure we only look at the lowest 4 bits
byte maskedValue = value & 0x0F;
Serial.print("Displaying Decimal: ");
Serial.print(value);
Serial.print(" | Binary: ");
Serial.println(maskedValue, BIN);
for (int i = 0; i < PIN_COUNT; i++) {
// Extract the i-th bit: shift right by i, then AND with 1
int bitState = (maskedValue >> i) & 0x01;
digitalWrite(LED_PINS[i], bitState);
}
}
void loop() {
// Hardcode the number 6 in binary code
byte targetNumber = 0b0110;
displayBinary(targetNumber);
delay(2000); // Hold the '6' pattern for 2 seconds
// Cycle through 0 to 15 for demonstration
for (byte i = 0; i <= 15; i++) {
displayBinary(i);
delay(500);
}
Serial.println("Cycle complete. Returning to 6...");
delay(1000);
}
Debugging: First Three Things to Check When It Fails
When working with bitwise logic and ESP32 GPIOs, things occasionally go wrong. If your board boots but the LEDs stay dark, or the serial monitor spits out red text, follow this ranked troubleshooting path.
1. Check for the ESP-IDF GPIO Output Error
If your serial monitor prints the following exact error string:
E (142) gpio: gpio_set_level(226): GPIO output gpio_num error
The Cause: You mapped one of your binary bits to an input-only pin (GPIO 34, 35, 36, or 39) or a pin strapped to the onboard SPI flash (like GPIO 6-11). Line 226 in the ESP-IDF gpio.c source code specifically checks GPIO_IS_VALID_OUTPUT_GPIO and throws this error if you try to drive an invalid pin HIGH or LOW.
The Fix: Verify your LED_PINS array. Stick to safe output pins like 25, 26, 27, 14, 12, 13, or 32, 33.
2. Verify the Octal vs. Binary Prefix Trap
The Symptom: The code compiles, but the LEDs display the pattern for decimal 72 (which overflows 4 bits) instead of 6, or the compiler throws error: invalid digit '8' in octal constant.
The Cause: You typed byte targetNumber = 0110; instead of 0b0110. In C/C++, a leading zero tells the compiler the number is octal (base-8). Octal 110 equals decimal 72.
The Fix: Always use 0b for binary (e.g., 0b0110) or 0x for hex (e.g., 0x06).
3. Measure the Forward Voltage and Resistor Drop
The Symptom: The serial monitor shows the correct binary output, but the LEDs are dim or completely off.
The Cause: The ESP32-WROOM-32E outputs 3.3V on its GPIO pins, not 5V. If you are using high-forward-voltage LEDs (like some blue or white ones at 3.2V), 3.3V minus the resistor drop leaves almost no current to light the die.
The Fix: Grab your multimeter. Measure the voltage across the LED. If it's below 2.5V, swap to standard red LEDs (1.8V - 2.2V Vf) or drop the resistor value to 100Ω to allow more current flow from the 3.3V rail.
Extending and Simplifying the Build
Once you have 6 in binary code reliably displayed on 4 bits, you will likely want to scale the project. Here is how to adapt the hardware based on your end goal.
If you don't need physical LEDs and just want to debug bitwise math in your firmware, strip out the
pinMode and digitalWrite functions. Rely entirely on Serial.println(val, BIN);. This frees up GPIOs for I2C sensors and reduces your code footprint by roughly 400 bytes of compiled flash.
How to Extend to 8-Bit (00000110):
To display a full 8-bit byte, you need 8 GPIOs. The ESP32 has enough, but routing 8 wires on a breadboard gets messy. The professional approach is to use a 74HC595 Serial-in, Parallel-out Shift Register.
By using the shift register, you only consume 3 ESP32 pins (Data, Clock, Latch). You send the entire 8-bit binary representation of 6 (0b00000110) out via SPI or bit-banging, and the 74HC595 latches it to its 8 output pins simultaneously. This is exactly how industrial PLCs and digital signage manage multi-bit states without exhausting microcontroller I/O.
Frequently Asked Questions
How do you write 6 in binary code for an 8-bit microcontroller register?
In an 8-bit system (like an AVR ATmega328P or an 8-bit ESP32 register), 6 is written as 00000110. When configuring hardware registers directly via C++ pointers, you would typically use the hexadecimal equivalent 0x06 or the binary literal 0b00000110. The leading zeros are technically optional for the compiler's math engine, but they are mandatory for human readability and ensuring you don't accidentally overwrite adjacent bitmasks in a control register.
Why does my C++ compiler throw an octal error when I type 0110 for binary 6?
This happens because C and C++ compilers use a leading zero to denote an octal (base-8) number. When you type 0110, the compiler reads it as $(1 \times 8^2) + (1 \times 8^1) + (0 \times 8^0)$, which equals decimal 72. If you try to type 08 or 09 thinking it's binary, the compiler will halt with an invalid digit in octal constant error, because 8 and 9 do not exist in base-8. Always use the 0b prefix for binary.
What is the hexadecimal equivalent of 6 in binary code, and when should I use it?
The hexadecimal equivalent of binary 0110 is 0x6 (or 0x06). You should use hexadecimal instead of binary when dealing with byte-wide (8-bit) or word-wide (16/32-bit) data. Reading a 32-bit binary string like 0b00000000000000000000000000000110 is prone to human counting errors. Reading 0x00000006 is immediate and maps cleanly to memory addresses and I2C device registers.
Can I use the number 6 in binary code to set an I2C address?
Yes, but with a caveat. Many I2C sensors (like the MPU6050 or BME280) have a base address that is modified by pulling an address pin HIGH or LOW. If a datasheet states the base address is 0x68 and the LSB is determined by the SDO pin, pulling SDO low results in 0x68 (binary 01101000). Pulling it high shifts the LSB, making it 0x69 (binary 01101001). You are manipulating the binary code of the address directly via hardware wiring.






