30
8
Write a program that checks if the integer is a power of 2.
Sample input:
8
Sample output:
Yes
Sample input:
10
Sample output:
No
Rules:
Don't use
+,-operations.Use some sort of input stream to get the number. Input is not supposed to be initially stored in a variable.
The shortest code (in bytes) wins.
You can use any truthy/falsy response (for instance, true/false). You may assume that input number is greater than 0.
Simple answer:log2 then check if it has numbers under the fp – Matthew Roh – 9 years ago
Can input be unary? – Comrade SparklePony – 9 years ago
1Is it also allowed to output "true" instead of "yes" and "false" instead of "no"? – ProgramFOX – 12 years ago
2Yes, you can use any positive/negative response. Question is updated. – gthacoder – 12 years ago
@gthacoder You may want to clarify input requirements, I'm almost feeling like I'm cheating using a variable with the value to check as input. Is that ok, seeing that GolfScript passes the value on the stack? – Joachim Isaksson – 12 years ago
1The
predfunction, when applied to an integer n, returns n - 1. Are functions such as this, which are thin disguises around the forbidden operator, also forbidden? – Wayne Conrad – 12 years ago1@Wayne just like golfscript's
), or most c-based languages'--. – Doorknob – 12 years ago1Can we output 1 or 0? 'T' or 'F'? – Mark Plotnick – 12 years ago
1@JoachimIsaksson Good point. Input is supposed to be typed by user. Question is updated. – gthacoder – 12 years ago
Requiring the input from a stream automatically penalizes languages such as
AS3orJavawhich don't have that sort of capability within arm's reach. Allowing "input" variables would level the playing field. – IQAndreas – 12 years ago1What is the range of valid input? This is important because a number of answers here break if the input is
0. – Peter Taylor – 12 years agoDon't worry about
0. Input number is supposed to be greater than0. Question is updated. – gthacoder – 12 years ago@gthacoder Is
--alowed for C(++)? And unary-(negate)? What about negative constants? (-1) – orlp – 12 years ago1@nightcracker Question is created with idea that you can't use the fact that if
n & (n - 1)is equal to0, thennis a power of 2 (reason why+,-are forbidden). Otherwise, you can use any operators. – gthacoder – 12 years ago@gthacoder But see my answer, I don't use
+or-and it still works. I do use-1though. – orlp – 12 years agoI am not seeing the need for plus or minus so much. It seems pretty easy to do it without. – Tim Seguine – 12 years ago
Can you use return value instead of explicitly printing? – Kevin – 12 years ago
It is supposed to be a complete program with input and output. – gthacoder – 12 years ago
If we use
+in a regex, is that OK? – O-I – 12 years ago@O-I
+OPERATION is forbidden, so+symbol in a regular expression is OK. I found you rewrote your answer without+anyway. – gthacoder – 12 years ago@gthacoder My C answer is smaller than vershov's. – orlp – 12 years ago
@nightcracker Yes, you are right. My bad. Sorry. Question is updated. – gthacoder – 12 years ago
@gthacoder Add some bonus for code that works also for 0 (return False). Many answers do not work for this case (should crash if use log, or return wrong answer). – Gari BN – 12 years ago
Can you add Perl 6? It's a separate language to Perl 5, even with confusing name. – Konrad Borowski – 12 years ago
@xfix Good point. They differ fundamentally indeed. Question is updated. – gthacoder – 12 years ago
if -1 as a constant is allowed I can get the javascript down to 28 characters (cannot post, need reputation!) alert(((a=prompt())&a*-1)>1) – serakfalcon – 12 years ago
1alert(!((a=prompt())&(a/3))) is also 28 characters and avoids the - sign altogether – serakfalcon – 12 years ago
Is there an upper limit to the range of valid inputs? – Khuldraeseth na'Barya – 8 years ago
2I know we're 3 years in the future now, but "+/- operators" is non-observable, or at the very least weakly defined. – ATaco – 8 years ago