Challenge: Checking Specific Bits in an Unsigned Integer
Write a function that returns True if the specific number of bits starting from the specific position are on, and False otherwise.
We'll cover the following
Consider an unsigned integer in which the rightmost bit is numbered as 0
. Write a function checkbits(x, p, n)
which returns True
if all n
bits starting from position p
are on, and False
otherwise. For example, checkbits(x, 4, 3)
will return True
if bits 4
, 3
, and 2
are 1
in number x
:
- Input: A number
num
, positionp
, and number of bitsn
. - Output: The
display_bits(n)
function prints the number in binary form and thecheckbits(x, p, n)
function returnsTrue
if alln
bits starting from positionp
are on, andFalse
otherwise.
Get hands-on with 1400+ tech skills courses.