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.

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, position p, and number of bits n.
  • Output: The display_bits(n) function prints the number in binary form and the checkbits(x, p, n) function returns True if all n bits starting from position p are on, and False otherwise.

Get hands-on with 1400+ tech skills courses.