In this code good use of bit manipulation and shift operation is done..Let us se iteration by iteration..
In 1st iteration ,
Word = 0x ABC meaning in hexadecimal notation
n = 0 initially
Now the operation word & 0xF will do bitwise OR operation with the current value of word which is ABC as of now..
So 0xF can also be written by 0x 00F as we can always 0 on left before decimal point without loss of any value ..So
ABC AND 00F will yield C only..So index of num_set_bits = C = 12
Hence num_set_bits[12] = 2 , so n = n + 2 = 2
And word is right shifted 4 times which is equivalent to division by 16 or in other words shifting right by one hexadecimal digit..
So word = AB now
In 2nd iteration :
AB AND 0F = B
So n = n + num_set_bits[11]
= 2 + 3 = 5
And word = AB >> 4 = A
In 3rd iteration :
A AND F = A
So n = n + num_set_bits [10]
= 5 + 2
= 7
Hence the final answer is 7 ..