Check Whether the Two Numbers Differ at One Bit Position Only - GeeksforGeeks

Check Whether the Two Numbers Differ at One Bit Position Only

Last Updated : 2 Aug, 2026

Given two non-negative integers a and b, find if their binary representations differ at exactly one bit position.

Examples: 

Input: a = 13, b = 9
Output: true
Explanation: The binary representations of 13 (1101) and 9 (1001) differ at exactly one bit position.

Input: a = 15, b = 8
Output: false
Explanation: The binary representations of 15 (1111) and 8 (1000) differ at more than one bit position.

Try It Yourself
redirect icon

[Naive Approach] Compare Bits One by One - O(log(max(a, b))) Time and O(1) Space

The idea is to compare the corresponding bits of both numbers one by one.

Working of Approach:

  • Initialize a variable count to store the number of differing bits.
  • Compare the least significant bit of both numbers using & 1.
  • Increment count whenever the bits are different.
  • Right shift both numbers to process the next bit.
  • Return true if count is exactly 1; otherwise, return false.
C++
#include <bits/stdc++.h>
using namespace std;

bool differOneBit(int a, int b)
{

    int count = 0;

    while (a > 0 || b > 0)
    {

        // Compare the least significant bits.
        if ((a & 1) != (b & 1))
            count++;

        // More than one differing bit.
        if (count > 1)
            return false;

        // Move to the next bit.
        a >>= 1;
        b >>= 1;
    }

    // Check if exactly one bit differs.
    return count == 1;
}

int main()
{

    int a = 13, b = 9;

    if (differOneBit(a, b))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
public class GFG {

    public static boolean differOneBit(int a, int b)
    {

        int count = 0;

        while (a > 0 || b > 0) {

            // Compare the least significant bits.
            if ((a & 1) != (b & 1))
                count++;

            // More than one differing bit.
            if (count > 1)
                return false;

            // Move to the next bit.
            a >>= 1;
            b >>= 1;
        }

        // Check if exactly one bit differs.
        return count == 1;
    }

    public static void main(String[] args)
    {

        int a = 13, b = 9;

        if (differOneBit(a, b))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
def differOneBit(a, b):

    count = 0

    while a > 0 or b > 0:

        # Compare the least significant bits.
        if (a & 1) != (b & 1):
            count += 1

        # More than one differing bit.
        if count > 1:
            return False

        # Move to the next bit.
        a >>= 1
        b >>= 1

    # Check if exactly one bit differs.
    return count == 1


if __name__ == '__main__':
    a = 13
    b = 9
    if differOneBit(a, b):
        print('true')
    else:
        print('false')
C#
using System;

class GFG {

    static bool differOneBit(int a, int b)
    {

        int count = 0;

        while (a > 0 || b > 0) {

            // Compare the least significant bits.
            if ((a & 1) != (b & 1))
                count++;

            // More than one differing bit.
            if (count > 1)
                return false;

            // Move to the next bit.
            a >>= 1;
            b >>= 1;
        }

        // Check if exactly one bit differs.
        return count == 1;
    }

    static void Main()
    {

        int a = 13, b = 9;

        if (differOneBit(a, b))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
function differOneBit(a, b)
{

    let count = 0;

    while (a > 0 || b > 0) {

        // Compare the least significant bits.
        if ((a & 1) !== (b & 1))
            count++;

        // More than one differing bit.
        if (count > 1)
            return false;

        // Move to the next bit.
        a >>= 1;
        b >>= 1;
    }

    // Check if exactly one bit differs.
    return count === 1;
}

// Driver Code
let a = 13, b = 9;
if (differOneBit(a, b))
    console.log("true");
else
    console.log("false");

Output
true

[Expected Approach] Using XOR - O(1) Time and O(1) Space

The idea is to use XOR to find the differing bit positions. If the XOR result has exactly one set bit, then the two numbers differ at exactly one bit position.

Let us understand with an example:
Input: a = 13, b = 9

  • Compute the XOR of the two numbers: 13 ^ 9 = 4 (1101 ^ 1001 = 0100).
  • The XOR result 4 is non-zero, so the numbers are not identical.
  • Check (4 & (4 - 1)) = (4 & 3) = (0100 & 0011) = 0000.
  • Since the result is 0, the XOR contains exactly one set bit.
  • Therefore, the binary representations of 13 and 9 differ at exactly one bit position, so the output is true.
C++
#include <bits/stdc++.h>
using namespace std;

bool differOneBit(int a, int b)
{
    int x = a ^ b;

    // Check if the numbers are identical.
    if (x == 0)
    {
        return false;
    }

    // Check if exactly one bit differs.
    if ((x & (x - 1)) == 0)
    {
        return true;
    }

    return false;
}

int main()
{

    int a = 13, b = 9;

    if (differOneBit(a, b))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
public class GFG {
    public static boolean differOneBit(int a, int b)
    {
        int x = a ^ b;

        // Check if the numbers are identical.
        if (x == 0) {
            return false;
        }

        // Check if exactly one bit differs.
        if ((x & (x - 1)) == 0) {
            return true;
        }

        return false;
    }

    public static void main(String[] args)
    {
        int a = 13, b = 9;

        if (differOneBit(a, b))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
def differOneBit(a, b):
    x = a ^ b

    # Check if the numbers are identical.
    if x == 0:
        return False

    # Check if exactly one bit differs.
    if (x & (x - 1)) == 0:
        return True

    return False


if __name__ == '__main__':
    a = 13
    b = 9

    if differOneBit(a, b):
        print('true')
    else:
        print('false')
C#
using System;

class GFG {
    static bool differOneBit(int a, int b)
    {
        int x = a ^ b;

        // Check if the numbers are identical.
        if (x == 0) {
            return false;
        }

        // Check if exactly one bit differs.
        if ((x & (x - 1)) == 0) {
            return true;
        }

        return false;
    }

    static void Main()
    {
        int a = 13, b = 9;

        if (differOneBit(a, b))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
function differOneBit(a, b)
{
    let x = a ^ b;

    // Check if the numbers are identical.
    if (x === 0) {
        return false;
    }

    // Check if exactly one bit differs.
    if ((x & (x - 1)) === 0) {
        return true;
    }

    return false;
}

// Driver Code
let a = 13, b = 9;

if (differOneBit(a, b)) {
    console.log("true");
}
else {
    console.log("false");
}

Output
true
Comment