logo CodeStepByStep logo

unsetRightmostBit

Language/Type: C++ bit algorithms

Write a function named unsetRightmostBit that accepts as a parameter an integer n and returns a new integer k whose binary representation is the same as n's but with the rightmost 1 bit set to 0. For example, if the call is unsetRightmostBit(44), the binary representation of 44 is 101100, so you should return the integer whose binary representation is 101000, which is 40.

You should solve this problem using bitwise operators and arithmetic. Do not use any loops, if/else statements, or collections to solve the problem. Also do not convert the integer to a string. Assume that the integers are stored using ones' complement binary notation.

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.