Binary ↔ Decimal Conversion
Convert between binary (base-2) and decimal (base-10) number systems. Fundamental for computer science. Binary uses only 0 and 1, decimal uses 0-9.
Visualization
Interactive visualization for Binary ↔ Decimal Conversion
Interactive visualization with step-by-step execution
Implementation
1class BinaryDecimalConverter {
2 decimalToBinary(decimal: number): string {
3 if (decimal === 0) return '0';
4 if (decimal < 0) return '-' + this.decimalToBinary(-decimal);
5
6 let binary = '';
7 while (decimal > 0) {
8 binary = (decimal % 2) + binary;
9 decimal = Math.floor(decimal / 2);
10 }
11 return binary;
12 }
13
14 binaryToDecimal(binary: string): number {
15 let decimal = 0;
16 let power = 0;
17
18 // Handle negative
19 const isNegative = binary.startsWith('-');
20 binary = binary.replace('-', '');
21
22 // Process from right to left
23 for (let i = binary.length - 1; i >= 0; i--) {
24 if (binary[i] === '1') {
25 decimal += Math.pow(2, power);
26 }
27 power++;
28 }
29
30 return isNegative ? -decimal : decimal;
31 }
32
33 // Using built-in methods
34 decimalToBinaryBuiltin(decimal: number): string {
35 return decimal.toString(2);
36 }
37
38 binaryToDecimalBuiltin(binary: string): number {
39 return parseInt(binary, 2);
40 }
41}Deep Dive
Theoretical Foundation
Decimal to binary: repeatedly divide by 2, record remainders in reverse. Binary to decimal: multiply each bit by 2^position and sum. Position 0 is rightmost. Example: 1011₂ = 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 11₁₀
Complexity
Time
O(log n)
O(log n)
O(log n)
Space
O(log n)
Applications
Industry Use
Computer programming and debugging
Digital circuit design and analysis
Network protocol implementation
Embedded systems programming
Cryptography and security systems
Data compression algorithms
Assembly language programming
Computer graphics and image processing
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.