ProgrammingVery hardGATE CS & IT · Infosys

What is printed?

What is printed?
def rev8(x):
    r = 0
    for _ in range(8):
        r = (r << 1) | (x & 1)
        x >>= 1
    return r
print(rev8(1), rev8(0b00010110), format(rev8(0b00010110), '08b'))
  1. A. 128 104 01101000
  2. B. 128 104 00010110
  3. C. 64 104 01101000
  4. D. 128 22 01101000

Correct answer: A. 128 104 01101000

The loop takes the lowest bit of x and pushes it onto the bottom of r, reversing eight bits. The bit 1 moves to position 7, which is 128, and 00010110 reverses to 01101000, which is 104.

More Python Programming questions

Exams that ask this topic