ProgrammingHardCapgemini · Infosys pattern

What will be the output of the following pseudocode (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR, & is bitwise AND.)

What will be the output of the following pseudocode (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR, & is bitwise AND.)
Function hamming(Integer a, Integer b)
    Integer x = a xor b
    Integer c = 0
    while (x != 0)
        c = c + (x & 1)
        x = x >> 1
    end while
    return c
End Function

Print hamming(7, 10)
  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 1

Correct answer: B. 3

7=0111, 10=1010. XOR gives 1101, which has three 1-bits, so the Hamming distance is 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic