Write a function that takes an int and returns the number of bits that are on in that int.
For example countBits( 1 ) would return 1 0000...00000001
countBits( 3 ) would return 2 0000...00000010
countBits( 7 ) would return 3 0000...00000111
etc... (You get the picture)
(Make sure to calculate how many bits are used to represent an
int on your system.)
Also display the bits that are on.
Rather simple function, but may be fun for some programming newbies
to try. Saw it on a discussion of job interview questions on some
board or another.
For example countBits( 1 ) would return 1 0000...00000001
countBits( 3 ) would return 2 0000...00000010
countBits( 7 ) would return 3 0000...00000111
etc... (You get the picture)
(Make sure to calculate how many bits are used to represent an
int on your system.)
Also display the bits that are on.
Rather simple function, but may be fun for some programming newbies
to try. Saw it on a discussion of job interview questions on some
board or another.