# Shortest python3 code for: Find the difference between a number and a power of two not exceeding a given number

Mathematically, you need to find the power of two not exceeding the given number, read from the standard input stream, and output their difference in the standard output stream. My python3 solution takes 41 characters, but the leader’s solution is only 40 characters. Who can shorten the code?

Newlines, spaces and tabs are not included while counting code length.

x = int(input())
print(x - 2 ** (len(bin(x)) - 3))

• Your code (with spaces removed) is 42 characters. Did you forget to count the newline, perhaps? – Dingus Jul 5 '20 at 12:31
• You should state that in the question. Not counting spaces/tabs/newlines is unusual on this site. – Dingus Jul 5 '20 at 12:35
• It is almost certainly a bad idea not to count newlines, spaces and tabs because somebody can encode a binary string using spaces and tabs, decode it to ASCII and evaluate it. – the default. Jul 5 '20 at 12:50
• Not counting whitespace in the score is not a good idea. That said, the task here is so simple that the naive approach appears to be shorter than any of the answers to that question, so I won't close as duplicate. – pppery Jul 5 '20 at 17:02
• @cairdcoinheringaahing The issue is that if you don't count whitespace there is a simple wrapper that solves any task in python in ~45 bytes of non-whitespace. Thus it doesn't matter what the task is the the score is always the same assuming that you can solve the problem. I agree with pppery here though that because this task is so simple that the wrapper method is not viable and thus this is not a duplicate. – Wheat Wizard Jul 5 '20 at 18:57

# 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!

• If Python 2 is allowed, this can be 37 bytes (not counting the space or newline). – Dingus Jul 5 '20 at 13:44
• @Dingus I think the int(...) is also not necessary in Python 2. – the default. Jul 5 '20 at 13:46
• Indeed, it looks like you're right. (I don't know Python well enough to say for sure.) – Dingus Jul 5 '20 at 13:50
• @mypronounismonicareinstate any idea to get 38?))) – Evgeny Jul 5 '20 at 18:10

# 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!

• The question doesn't specify the version of Python to be used so you could switch to 2 and drop the inner int(). – Shaggy Jul 5 '20 at 16:59
• @Shaggy ACMP only supports Python 3 (as far as I know). – the default. Jul 5 '20 at 17:22
• @Ad Hoc Garf Hunter, Great! – Evgeny Jul 5 '20 at 18:07
• "we need '0'+ if we want it to work on zero or one" - 0 is an invalid input anyway, since it's smaller than all powers of 2. – user2357112 supports Monica Jul 6 '20 at 11:34