Skip to content

ORIGINAL CODING QUESTION

Differing permission bits

Two access profiles are stored as nonnegative integers whose binary bits are permission flags. Count the bit positions at which the two profiles differ.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

Two integers a and b. Print the number of differing bits.

Constraints

0 ≤ a, b ≤ 1,000,000.

Example 1

Input

5 3

Output

2

5 is 101 and 3 is 011 in binary; the two highest of these bits differ.

Example 2

Input

12 12

Output

0

Identical profiles differ in no position.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

XOR sets a bit exactly where the two inputs differ.

Hint 2

After computing a XOR b, count its one bits.

Hint 3

Repeatedly clear the lowest set bit with x = x & (x - 1) and count how many times this runs before x becomes 0.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →