logo CodeStepByStep logo

bitsToFlip

Language/Type: C++ bit algorithms

Write a function named bitsToFlip that accepts as parameters two integers a and b and returns the number of bits that would need to be flipped from 0 to 1 or vice versa to turn a's binary representation into b's. For example, if the call is bitsToFlip(42, 108), the binary representation of 42 is 0101010 preceded by 25 zeros, and the binary representation of 108 is 1101100 preceded by 25 zeros. To convert the first into the second you would need to flip three bits. So your function should return 3.

You should solve this problem using bitwise operators and arithmetic. Do not use any collections or strings to solve the problem. You may assume that an int occupies 32 bits in memory.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.