DEV Community

Imtiaz ali
Imtiaz ali

Posted on

Binary and Decimal Conversion: The Developer's Practical Guide (Not Just Theory)

`---

description: "Understand binary-decimal conversion beyond textbook formulas — with real programming use cases, bitwise operation examples, IP subnetting, Unix permissions, and a free converter."
tags: JavaScript, beginners, computer science, webdev

Binary conversion is one of those topics that shows up in CS fundamentals and then seems to disappear from day-to-day work. Until it doesn't. And then you need to understand it properly, quickly, without wading through academic theory.

This guide is for working developers who need binary and decimal conversion for actual tasks: reading memory dumps, working with bitwise operators, debugging subnets, understanding file permissions. With a working converter at the end so you can verify every example.


The Core Concept in Two Minutes

Decimal (base 10): The number system you use every day. Each position is a power of 10.

plaintext
4 5 3
│ │ └── 3 × 10⁰ = 3 × 1 = 3
│ └────── 5 × 10¹ = 5 × 10 = 50
└────────── 4 × 10² = 4 × 100 = 400
─────
453

Binary (base 2): Exactly the same structure, but each position is a power of 2 instead of 10.

`plaintext
1 1 0 1
│ │ │ └── 1 × 2⁰ = 1 × 1 = 1
│ │ └────── 0 × 2¹ = 0 × 2 = 0
│ └────────── 1 × 2² = 1 × 4 = 4
└────────────── 1 × 2³ = 1 × 8 = 8
───
13

So: 1101₂ = 13₁₀
`

That's the whole conversion. Multiply each bit by its positional power of 2, sum the results.


Two Methods for Manual Conversion

Method 1: Positional (Right to Left)

Start from the rightmost bit (position 0), multiply each bit by 2 raised to its position, add everything up.

Example: Convert 10110₂

Bit Position 2ⁿ Value
0 0 1 0
1 1 2 2
1 2 4 4
0 3 8 0
1 4 16 16

Sum: 0 + 2 + 4 + 0 + 16 = 22


Method 2: Double Dabble (Left to Right, Faster for Long Strings)

Start from the leftmost bit. For each bit: double the running total, add the current bit.

Example: Convert 10110₂

plaintext
Start: 0
Bit 1: (0 × 2) + 1 = 1
Bit 0: (1 × 2) + 0 = 2
Bit 1: (2 × 2) + 1 = 5
Bit 1: (5 × 2) + 1 = 11
Bit 0: (11 × 2) + 0 = 22

Result: 22

This method is easier for mental arithmetic on longer binary strings because you never need to calculate large powers of 2.


Decimal to Binary: Division Method

Divide by 2 repeatedly, recording remainders. Read remainders bottom to top.

Example: Convert 45₁₀

`plaintext
45 ÷ 2 = 22 remainder 1 ← LSB (least significant bit)
22 ÷ 2 = 11 remainder 0
11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1 ← MSB (most significant bit)

Read bottom to top: 1 0 1 1 0 1

Result: 45₁₀ = 101101₂
`

Verify: 32 + 0 + 8 + 4 + 0 + 1 = 45 ✓


Where This Actually Matters in Real Development

1. Bitwise Operators

Every JavaScript, Python, C, and Java developer uses these — even if they don't think about the binary underneath.

`javascript
let a = 13; // binary: 1101
let b = 10; // binary: 1010

// AND: 1101 & 1010 = 1000 = 8
console.log(a & b); // 8

// OR: 1101 | 1010 = 1111 = 15
console.log(a | b); // 15

// XOR: 1101 ^ 1010 = 0111 = 7
console.log(a ^ b); // 7

// Left shift: 1101 << 1 = 11010 = 26
console.log(a << 1); // 26

// Right shift: 1101 >> 1 = 0110 = 6
console.log(a >> 1); // 6
`

Left shift by 1 = multiply by 2. Right shift by 1 = integer divide by 2. This is why bit shifts are used for performance-critical arithmetic.

Real use case — checking if a number is even or odd without modulo:

`javascript
// Using bitwise AND with 1
// Even numbers always end in 0, odd numbers end in 1
function isEven(n) {
return (n & 1) === 0;
}

console.log(isEven(4)); // true (100 & 001 = 000)
console.log(isEven(7)); // false (111 & 001 = 001)
`

Real use case — feature flags with bitmasks:

`JavaScript
const PERMISSIONS = {
READ: 0b0001, // 1
WRITE: 0b0010, // 2
DELETE: 0b0100, // 4
ADMIN: 0b1000 // 8
};

let userPerms = PERMISSIONS.READ | PERMISSIONS.WRITE; // 0011 = 3

// Check if user has write permission
if (userPerms & PERMISSIONS.WRITE) {
console.log("User can write");
}

// Grant delete permission
userPerms |= PERMISSIONS.DELETE; // 0111 = 7

// Revoke write permission
userPerms &= ~PERMISSIONS.WRITE; // 0101 = 5
`

Bitmask permission systems are common in embedded systems, game development, and any context where memory efficiency matters.


2. Unix File Permissions

The chmod 755 you run every time you deploy? That's binary.

`shell
chmod 755 file.sh

7 = 111 = rwx (read, write, execute) — owner
5 = 101 = r-x (read, execute) — group

5 = 101 = r-x (read, execute) — others
`

The three octal digits (0–7) each represent a 3-bit binary number:

  • Bit 2 (4) = read
  • Bit 1 (2) = write
  • Bit 0 (1) = execute

So chmod 644 = 110 100 100 = owner can read/write, everyone else can only read. Makes perfect sense once you see the binary.

`bash

Common permission codes explained in binary:

777 = 111 111 111 = rwxrwxrwx (everyone full access)

755 = 111 101 101 = rwxr-xr-x (owner full, others read/exec)

644 = 110 100 100 = rw-r--r-- (owner read/write, others read)

600 = 110 000 000 = rw------- (owner read/write only)

`


3. IP Addresses and Subnetting

IPv4 addresses are 32-bit binary numbers, split into four 8-bit octets.

`plaintext
IP address: 192.168.1.100

Binary:
192 = 11000000
168 = 10101000
1 = 00000001
100 = 01100100

Full binary: 11000000.10101000.00000001.01100100
`

Subnet masks use this directly:

`plaintext
Subnet /24 = 24 ones followed by 8 zeros:
11111111.11111111.11111111.00000000
= 255.255.255.0

/25 = 11111111.11111111.11111111.10000000
= 255.255.255.128 (splits the last octet in half)
`

The / notation (CIDR) tells you how many leading 1s are in the mask. Understanding this in binary makes subnetting intuitive rather than mysterious.


4. Reading Memory Addresses and Hex

Hexadecimal is just a compact notation for binary. Every 4 bits = exactly 1 hex digit.

plaintext
Binary: 1111 1010
Hex: F A → 0xFA = 250 decimal

When you see memory addresses like 0x7fff5fbff6b8, those hex digits each represent 4 binary bits. This is why hex dominates in debuggers, hex editors, and assembly — it's the most human-readable form of binary.

javascript
// JavaScript handles all bases natively
parseInt('1101', 2) // binary to decimal: 13
parseInt('FA', 16) // hex to decimal: 250
(13).toString(2) // decimal to binary: "1101"
(250).toString(16) // decimal to hex: "fa"


5. Color Values in CSS/Design

RGB hex colors are binary at the bottom.

css
/* #FF5733 */
FF = 11111111 = 255 (red channel, maximum)
57 = 01010111 = 87 (green channel)
33 = 00110011 = 51 (blue channel)

When designers talk about 8-bit color depth, they mean each channel is one byte (8 bits), allowing 256 values (0–255) per channel, and 256³ = 16.7 million possible colors.


The Important Values to Memorize

You don't need to memorize all 256-byte values. Just know the powers of 2:

Power Value Why It Matters
2⁰ 1 Least significant bit
2
4
8
2⁴ 16 One hex digit (0–F)
2⁷ 128 Sign bit in signed 8-bit
2⁸ 256 Values in one byte (0–255)
2¹⁰ 1024 1 kilobyte ≠ 1000 bytes
2¹⁶ 65536 Max unsigned 16-bit int
2³² 4,294,967,296 IPv4 address space

The fact that 1KB = 1024 bytes (not 1000) is because 1024 = 2¹⁰ — memory naturally falls on binary boundaries.


Quick Converter

For anything beyond mental arithmetic, use OurToolkit's Binary to Decimal Converter — it shows the full step-by-step working for every conversion, both directions. Good for verifying your manual work or learning the process.


Floating Point: The Edge Case Everyone Hits Eventually

One thing this guide hasn't covered: decimal fractions in binary.

JavaScript
0.1 + 0.2 === 0.3 // false in JavaScript (and every IEEE 754 language)
0.1 + 0.2 // 0.30000000000000004

This happens because 0.1 in binary is infinitely repeating — like 1/3 in decimal. The computer truncates it at 64 bits, leaving a tiny error. Multiply that error across many calculations and it compounds.

This is why you never compare floats with ===, why financial calculations use integers (cents, not dollars), and why BigDecimal exists in Java. It's binary's inability to represent some decimal fractions exactly.

Binary is elegant for integer math. It gets complicated the moment fractions enter the picture.


What binary-related bugs have you hit in the wild? The floating point one gets everyone at least once — drop your story in the comments.
`

Top comments (0)