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 (it is supposed to be typed). Input is not supposed to be initially stored in a variable.
The shortest code (in bytes) wins.
P.S. You can use any positive/negative response (for instance, true
/false
). You may assume that input number is greater than 0
.
Thanks to you, guys, this question became very popular and got a bunch of different answers with interesting approaches, so I decided to append some info to make this thread readable. Code Golf is not only programming languages battle, it is also a challenge for different approaches within a single language. I decided to sum up interim results and note the leaders for every programming language for better navigation within the thread.
GTB - 17 characters - Timtech
GolfScript - 6 characters - Ilmari Karonen
PHP - 58 - 58 characters - Joachim Isaksson
JavaScript - 35 characters - copy
Python - 31 characters - boothby
Perl 6 - 17 characters - xfix
Perl 5 - 14 characters - Ilmari Karonen
Octave - 23 characters - Joachim Isaksson
APL - 7 characters - marinus
R - 11 characters - Sven Hohenstein
Mathematica - 21 characters - ybeltukov
K - 24 characters - Kyle Kanos
Ruby - 19 characters - O-I
Scheme - 40 characters - Sylwester
C - 48 characters - nightcracker
Haskell - 52 characters - Zeta
J - 14 characters - FireFly
D - 83 characters - rachet freak
C# - 58 characters - Merin Nakarmi
AutoHotkey - 45 characters - user13542
This table will be updated with new answers posted.
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 yesterday)
, or most c-based languages'--
. – Doorknob of Snow yesterday