logo CodeStepByStep logo


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.

Type your C++ solution code here:

This is a function exercise. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.