fork download
  1. def next_power_of_2(x):
  2. return 1 if x == 0 else 2**(x - 1).bit_length()
  3.  
  4. def prev_power_of_2(x):
  5. return next_power_of_2(x) >> 1
  6.  
  7.  
  8. print(', '.join(f'{x}:{next_power_of_2(x)}' for x in range(10)))
  9. print(', '.join(f'{x}:{prev_power_of_2(x)}' for x in range(10)))
Success #stdin #stdout 0.08s 14172KB
stdin
Standard input is empty
stdout
0:1, 1:1, 2:2, 3:4, 4:4, 5:8, 6:8, 7:8, 8:8, 9:16
0:0, 1:0, 2:1, 3:2, 4:2, 5:4, 6:4, 7:4, 8:4, 9:8