All problems
Reverse Bits
Problem
Reverse the lowest width bits of the integer n and return the result, where width is at least 1.
For example, reversing 1 in 8 bits gives 128 and reversing 6 in 3 bits gives 3. Bits above the given width are ignored.
Hint
Build the result by shifting left and appending the next bit of n from the bottom.
Test cases
case 1
in: [1,8]
expected: 128
case 2
in: [2,8]
expected: 64
case 3
in: [0,8]
expected: 0
case 4
in: [255,8]
expected: 255
case 5
in: [6,3]
expected: 3
case 6
in: [5,3]
expected: 5
Starter codePython
def reverse_bits(n, width):
# Your code here
pass6 test cases · run your code against them in the editor