Binary: The Foundation of All Computing
Every value stored and processed by a computer — integers, strings, images, programs — is ultimately represented as binary. Binary (base 2) uses only two symbols: 0 and 1. Each symbol is a bit (binary digit). Eight bits form a byte, the basic unit of storage. Understanding binary is essential for working with bitwise operations, network protocols, compression algorithms, and low-level systems programming.
How Binary-to-Decimal Conversion Works
Each binary digit represents a power of 2, from right to left. The rightmost bit is 2⁰ = 1. The next is 2¹ = 2. Then 2² = 4, 2³ = 8, and so on. To convert, multiply each bit by its positional power and sum the results.
Example: 11001010 → (1×128) + (1×64) + (0×32) + (0×16) + (1×8) + (0×4) + (1×2) + (0×1) = 128 + 64 + 8 + 2 = 202.
// JavaScript
parseInt('11001010', 2) // → 202
parseInt('1111 1111'.replace(/\s/g, ''), 2) // → 255
// Python
int('11001010', 2) # → 202
0b11001010 # → 202 (binary literal) Bitwise Operations in Practice
Bitwise operators (&, |, ^, ~, <<, >>) work directly on the binary representation. To check if a specific bit is set: value & (1 << n). To set a bit: value | (1 << n). To clear a bit: value & ~(1 << n). These operations are used in flags, permissions, protocol headers, and hardware register manipulation.
Linux file permissions use a 9-bit field (rwxrwxrwx). The octal 755 = binary 111 101 101. Reading the binary directly, you can see exactly which permission bits are set without mental arithmetic.
Binary in Network Protocols
IP addresses, subnet masks, and TCP flags are all binary values. The subnet mask 255.255.255.0 in binary is 11111111.11111111.11111111.00000000 — 24 ones followed by 8 zeros, which is why it is written as /24 in CIDR notation. TCP header flags (SYN, ACK, FIN, RST, PSH, URG) are individual bits in a 6-bit field.
Frequently Asked Questions
How do I convert binary to decimal in JavaScript?
Use parseInt(binaryString, 2). The second argument specifies base 2. For example, parseInt('11001010', 2) returns 202. You can also use Number('0b11001010') with the 0b prefix, which is valid ES6 syntax.
What is the decimal value of 11111111 in binary?
11111111 in binary equals 255 in decimal. All 8 bits set to 1 represents the maximum value of a single byte. In hex, this is 0xFF. This value appears frequently in networking (broadcast addresses, subnet masks) and graphics (max color channel value in RGB).
How many decimal values can 8 bits represent?
8 bits can represent 2^8 = 256 different values. As an unsigned integer, this is 0 to 255. As a signed integer (using two's complement), this is -128 to 127. This is why a byte is such a fundamental unit — it maps exactly to values like color channels (0-255) and ASCII characters.
What does it mean for a bit to be set or unset?
A bit that is '1' is said to be set (or high or on). A bit that is '0' is unset (or low or off). To check whether a specific bit is set in a value, use the bitwise AND operator: (value & (1 << position)) !== 0. This pattern is used in permission flags, protocol headers, and hardware registers.
What is the binary prefix 0b in programming?
The 0b prefix is used in modern programming languages to denote a binary literal in source code, similar to how 0x denotes hexadecimal. In Python, 0b11001010 is a valid integer literal equal to 202. In JavaScript (ES6+), 0b11001010 also equals 202. In C (since C11), you can use 0b as well.