Shortest python3 code for: Find the difference between a number and a power of two not exceeding a given number
Python 3, 39 bytes
print(int('0'+bin(int(input()))[3:],2))
Try it online!
Subtracting the largest power of two less than a number is the same as removing the first 1
from its binary representation.
Unfortunately python rather mysteriously errors on trying to convert the empty binary string to an int so we need '0'+
if we want it to work on zero or one. If we relax the requirements to only require two or more then we can remove 4 bytes.
Python 3, 35 bytes
print(int(bin(int(input()))[3:],2))
Try it online!
Python 3, 40 39 bytes (not counting the newline)
First I simplified \$2^{a-3}\$ to \$\frac{2^a}8\$, then I used the lower-precedence operators <<
and ^
so that //
can be replaced by -
(which saves a byte).
x=int(input())
print(1<<len(bin(x))-3^x)
Try it online!