logo CodeStepByStep logo

cmp_bits

Author: Cynthia Lee (on 2016/11/03)

Write a function named cmp_bits that accepts two int arguments a and b, and compares the number of "on" bits (number of 1s) between them. Returns a negative result if the bitwise representation of a has fewer 1s than b, a positive result if a has more 1s than b, and zero if both have the same number of 1s. For example, if a is 5 (binary 00000000000000000000000000000101) and b is 32 (binary 00000000000000000000000000010000), then cmp_bits returns any positive number (for example, it could return 1).

Type your C solution code here:


This is a function problem. 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

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

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.