41
4
There are clever ways of determining whether a number is a power of 2. That's no longer an interesting problem, so let's determine whether a given integer is an integer power of -2. For example:
-2 => yes: (-2)¹
-1 => no
0 => no
1 => yes: (-2)⁰
2 => no
3 => no
4 => yes: (-2)²
Rules
You may write a program or a function and use any of the standard methods of receiving input and providing output.
Your input is a single integer, and output must be a truthy value if the integer is an integer power of -2, and a falsy value otherwise. No other output (e.g. warning messages) is permitted.
The usual integer overflow rules apply: your solution must be able to work for arbitrarily large integers in a hypothetical (or perhaps real) version of your language in which all integers are unbounded by default, but if your program fails in practice due to the implementation not supporting integers that large, that doesn't invalidate the solution.
You may use any programming language, but note that these loopholes are forbidden by default.
Winning condition
This is a code-golf contest: the answer which has the fewest bytes (in your chosen encoding) is the winner.
2 => no
shouldn't this be yes? – user41805 – 2017-04-06T12:09:03.72717@KritixiLithos I don't see why it should. There is no integer
i
such that(-2)^i = 2
– Fatalize – 2017-04-06T12:10:40.7532Are the exponents positive or
-0.5
should be valid since it's 2^(-1). – Mr. Xcoder – 2017-04-06T12:13:47.3801@Mr.Xcoder, Since inputs are always integer values, a negative exponent won't be required (or possible). – Toby Speight – 2017-04-06T12:16:06.197
@Fatalize But literally
(-2)^i
might be 2... – Matthew Roh – 2017-04-06T12:44:47.1331@SIGSEGV maybe whereas
i
is not natural – Mr. Xcoder – 2017-04-06T12:55:09.887@Mr.Xcoder, I think SIGSEGV was being facetious, as Fatalize clearly wrote "integer
i
". It's just a play on i being the standard imaginary number. Don't get too serious about that! – Toby Speight – 2017-04-06T13:02:02.687@Fatalize silly me, I thought the challenge asked for a power of 2 instead – user41805 – 2017-04-06T14:39:16.820
How many bits is the input integer? – Jason C – 2017-04-06T14:50:01.243
2@Jason, as many as supported/natural in your language - see the third rule. And it's [tag:code-golf] because it needs an objective winning criterion to be on-topic here - "a pleasing solution" doesn't cut it (though I do like the Mathematica answer - that surprised me). – Toby Speight – 2017-04-06T14:55:05.553
One thing you could have done, which is reminiscent of some of the challenge contests we used to have in college, is limit the solution to a set of operations, e.g. bit shifts, bitwise operators, addition/subtraction/multiplication/division or something. And limit it to a certain language (most popular languages are similar in this regard). Then make it code-golf. Now that would have been proper treatment of this otherwise really great challenge. – Jason C – 2017-04-06T14:58:36.977
1
@TobySpeight I've proposed an alternate version of this challenge here.
– Jason C – 2017-04-06T16:07:28.457