Solution #1: Basically, the question asks you to figure out which bits are different in A and B. Example: in case of 32 and 3, answer is 3 as they have 3 bits which are different. Taking a XOR and then counting the no. of 1s is the simplest approach. Here is the code,
void findBits(int a, int b) { int c = a^b; int count=0; for(;c!=0; c=c>>1) count += c&1; printf("Number of bits required to convert int A to B is %d\n", count); }
No comments:
Post a Comment