Checking if decimals share at least two common 1 bits in JavaScript

We need to write a JavaScript function that takes two numbers and returns true if they have at least two common 1 bits in their binary representations at the same positions.

Problem

Given two decimal numbers, we want to check if their binary representations share at least two 1 bits at the same index positions. For example, if we have numbers 10 (binary: 1010) and 15 (binary: 1111), we need to compare bit by bit and count matching 1s.

Algorithm Approach

The solution involves converting both numbers to binary strings, aligning them by removing extra leading bits from the longer string, then comparing each bit position to count common 1s.

Example

Let's implement and test the function:

const num1 = 10;
const num2 = 15;

const checkBits = (num1 = 1, num2 = 1) => {
    let c = num1.toString(2).split('');
    let d = num2.toString(2).split('');
    
    // Align binary strings by trimming the longer one
    if(c.length > d.length){
        c = c.slice(c.length - d.length);
    }else{
        d = d.slice(d.length - c.length);
    }
    
    let count = 0;
    for(let i = 0; i < d.length; i++){
        if(c[i] === "1" && d[i] === "1"){
            count++;
        }
    }
    
    return count > 1;
};

console.log(`Binary of ${num1}: ${num1.toString(2)}`);
console.log(`Binary of ${num2}: ${num2.toString(2)}`);
console.log(`Common 1 bits: ${checkBits(num1, num2)}`);
Binary of 10: 1010
Binary of 15: 1111
Common 1 bits: true

How It Works

The function works by:

  • Converting both numbers to binary strings using toString(2)
  • Splitting into character arrays for easier manipulation
  • Aligning the binary representations by removing leading bits from the longer string
  • Iterating through each bit position and counting matches where both bits are "1"
  • Returning true if count exceeds 1

Alternative Implementation Using Bitwise Operations

Here's a more efficient approach using bitwise AND operation:

const checkBitsOptimized = (num1, num2) => {
    let commonBits = num1 & num2; // Bitwise AND
    let count = 0;
    
    // Count number of 1s in the result
    while(commonBits > 0) {
        count += commonBits & 1;
        commonBits >>= 1;
    }
    
    return count > 1;
};

console.log("Using bitwise approach:");
console.log(checkBitsOptimized(10, 15)); // true
console.log(checkBitsOptimized(5, 3));   // false
console.log(checkBitsOptimized(7, 14));  // true
Using bitwise approach:
true
false
true

Comparison

Method Time Complexity Space Complexity Readability
String Conversion O(log n) O(log n) High
Bitwise Operations O(log n) O(1) Medium

Conclusion

Both approaches solve the problem effectively. The string method is more intuitive for beginners, while the bitwise approach is more memory-efficient and demonstrates advanced JavaScript bit manipulation techniques.

Updated on: 2026-03-15T23:19:00+05:30

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements