InfraHub
Back to Blog
Developer
DevOps Engineer

Binary and Hexadecimal for Developers: A Practical Guide

Understand why binary and hexadecimal matter in software development, how number base conversion works, and when to use each system.

Binary and Hexadecimal for Developers: A Practical Guide

Decimal is intuitive for humans. Binary and hexadecimal are the native languages of computers. As a developer, fluency in number base conversion isn't optional — it's the difference between understanding what's happening and guessing.

Why Binary Matters

Computers operate on bits: values that are either 0 or 1. Everything — memory addresses, network packets, file permissions, color values, character encodings — is ultimately binary.

Bitwise Operations

Bitwise operations work directly on the binary representation:

// Check if a number is even (test least significant bit)
const isEven = (n) => (n & 1) === 0;

// Set bit 3
const withBit3 = value | (1 << 3);

// Clear bit 3
const withoutBit3 = value & ~(1 << 3);

// Toggle bit 3
const toggledBit3 = value ^ (1 << 3);

File Permissions (Unix)

Unix permissions are a 3-bit field per role (owner, group, others):

chmod 755 = 111 101 101 (binary) = rwxr-xr-x
chmod 644 = 110 100 100 (binary) = rw-r--r--

Understanding binary makes these permissions immediately readable.

Why Hexadecimal Matters

Hexadecimal (base 16) is compact binary. Each hex digit represents exactly 4 bits, so a byte (8 bits) maps cleanly to two hex digits. This is why hex is ubiquitous:

  • Memory addresses: 0x7fff5fbff8e0
  • Colors in CSS: #FF5733
  • Hash values: sha256: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
  • MAC addresses: 00:1A:2B:3C:4D:5E
  • UUID values: 550e8400-e29b-41d4-a716-446655440000

Number Base Conversion

Decimal to Binary

Repeatedly divide by 2 and collect remainders (read bottom to top):

42 ÷ 2 = 21 r 0
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

42 in binary = 101010

Binary to Hexadecimal

Group binary digits into nibbles (4 bits) from right, then map each nibble:

Binary:  1010 1111 0011
Hex:        A    F    3
Result: 0xAF3

Hex-to-binary nibble map:

Hex Binary
0 0000
1 0001
... ...
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

Decimal to Hexadecimal

255 ÷ 16 = 15 r 15 (F)
 15 ÷ 16 =  0 r 15 (F)

255 = 0xFF

Practical Examples

IP Addresses in Hex

Subnet masks are cleaner in hex:

255.255.255.0  = 0xFFFFFF00
255.255.255.128 = 0xFFFFFF80

Character Encoding

ASCII 'A' is decimal 65, binary 01000001, hex 0x41. Knowing this helps when reading hex dumps:

echo -n "AB" | xxd
# 00000000: 4142  AB

Bitmasks in Application Code

# Feature flags using bitmasks
FEATURE_DARK_MODE    = 0b00000001  # 0x01
FEATURE_BETA_ACCESS  = 0b00000010  # 0x02
FEATURE_ADMIN        = 0b00000100  # 0x04

user_flags = 0b00000101  # dark mode + admin

has_admin = bool(user_flags & FEATURE_ADMIN)  # True

Convert Numbers Instantly

When you need quick number base conversions during development or debugging, the Base Converter on InfraHub converts between binary, decimal, hexadecimal, and octal in real time. Enter any value in any base and see all representations simultaneously — useful when reading kernel docs, analyzing network captures, or working with binary protocols.

Share Feedback

We read every message