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 – 2017-02-09T03:59:26.767
Can input be unary? – Comrade SparklePony – 2017-04-06T17:35:44.657
1Is it also allowed to output "true" instead of "yes" and "false" instead of "no"? – ProgramFOX – 2014-01-04T14:38:56.463
2Yes, you can use any positive/negative response. Question is updated. – gthacoder – 2014-01-04T14:42:33.513
@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 – 2014-01-04T15:35:50.010
1The
pred
function, 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 – 2014-01-04T15:35:54.7301@Wayne just like golfscript's
)
, or most c-based languages'--
. – Doorknob – 2014-01-04T15:38:06.5871Can we output 1 or 0? 'T' or 'F'? – Mark Plotnick – 2014-01-04T15:43:33.603
1@JoachimIsaksson Good point. Input is supposed to be typed by user. Question is updated. – gthacoder – 2014-01-04T15:47:17.000
Requiring the input from a stream automatically penalizes languages such as
AS3
orJava
which don't have that sort of capability within arm's reach. Allowing "input" variables would level the playing field. – IQAndreas – 2014-01-04T19:37:13.2131What is the range of valid input? This is important because a number of answers here break if the input is
0
. – Peter Taylor – 2014-01-04T21:01:03.243Don't worry about
0
. Input number is supposed to be greater than0
. Question is updated. – gthacoder – 2014-01-04T21:14:45.840@gthacoder Is
--
alowed for C(++)? And unary-
(negate)? What about negative constants? (-1
) – orlp – 2014-01-04T21:18:32.6571@nightcracker Question is created with idea that you can't use the fact that if
n & (n - 1)
is equal to0
, thenn
is a power of 2 (reason why+
,-
are forbidden). Otherwise, you can use any operators. – gthacoder – 2014-01-04T21:35:48.453@gthacoder But see my answer, I don't use
+
or-
and it still works. I do use-1
though. – orlp – 2014-01-04T21:36:48.203I am not seeing the need for plus or minus so much. It seems pretty easy to do it without. – Tim Seguine – 2014-01-04T22:33:59.797
Can you use return value instead of explicitly printing? – Kevin – 2014-01-04T23:25:09.307
It is supposed to be a complete program with input and output. – gthacoder – 2014-01-04T23:41:19.287
If we use
+
in a regex, is that OK? – O-I – 2014-01-05T01:21:51.077@O-I
+
OPERATION is forbidden, so+
symbol in a regular expression is OK. I found you rewrote your answer without+
anyway. – gthacoder – 2014-01-05T03:22:42.870@gthacoder My C answer is smaller than vershov's. – orlp – 2014-01-05T06:39:38.383
@nightcracker Yes, you are right. My bad. Sorry. Question is updated. – gthacoder – 2014-01-05T06:53:37.530
@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 – 2014-01-05T08:41:15.913
Can you add Perl 6? It's a separate language to Perl 5, even with confusing name. – Konrad Borowski – 2014-01-05T21:13:36.317
@xfix Good point. They differ fundamentally indeed. Question is updated. – gthacoder – 2014-01-05T21:25:17.300
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 – 2014-01-07T07:08:26.467
1alert(!((a=prompt())&(a/3))) is also 28 characters and avoids the - sign altogether – serakfalcon – 2014-01-07T10:09:53.423
Is there an upper limit to the range of valid inputs? – Khuldraeseth na'Barya – 2017-09-12T22:09:45.123
2I know we're 3 years in the future now, but "+/- operators" is non-observable, or at the very least weakly defined. – ATaco – 2017-09-12T22:57:02.710