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'))- A. 128 104 01101000
- B. 128 104 00010110
- C. 64 104 01101000
- 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
- What is printed? python def maj(a): cand, cnt = None, 0 for x in a: if cnt == 0: cand = x cnt += 1 if x == cand else -1 return cand…
- What is printed? python print(list(dict.fromkeys([3, 1, 3, 2, 1])), sorted(set([3, 1, 3, 2, 1]))[::-1])
- What is printed? python def sortcount(a): if len(a) <= 1: return a, 0 m = len(a) // 2 l, x = sortcount(a[:m]) r, y = sortcount(a[m:]) out…
- What is printed? python print([10, 20][True], [10, 20][False])
- What is printed? python print(repr('ab' -1), [1] 0, 'a' 3 == 'aaa')
- What is printed? python print(all([]), any([]), all([0, 1]), any([0, '']))