30
\$\begingroup\$

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.

\$\endgroup\$

closed as unclear what you're asking by Sriotchilism O'Zaic, 0 ', ATaco, Stephen, Steadybox Sep 13 '17 at 1:18

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Is it also allowed to output "true" instead of "yes" and "false" instead of "no"? \$\endgroup\$ – ProgramFOX Jan 4 '14 at 14:38
  • 2
    \$\begingroup\$ Yes, you can use any positive/negative response. Question is updated. \$\endgroup\$ – gthacoder Jan 4 '14 at 14:42
  • 1
    \$\begingroup\$ The 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? \$\endgroup\$ – Wayne Conrad Jan 4 '14 at 15:35
  • 1
    \$\begingroup\$ @Wayne just like golfscript's ), or most c-based languages' --. \$\endgroup\$ – Doorknob Jan 4 '14 at 15:38
  • 2
    \$\begingroup\$ I know we're 3 years in the future now, but "+/- operators" is non-observable, or at the very least weakly defined. \$\endgroup\$ – ATaco Sep 12 '17 at 22:57

69 Answers 69

0
\$\begingroup\$

Perl 69

$_=<>;chomp;$s=1;while($s<$_){$s*=2;$r="Yes" if $s==$_}print $r||"No"
\$\endgroup\$
  • 1
    \$\begingroup\$ 58: $_=<>;chomp;++$s;($s*=2)==$_&&($r=Yes)while$s<$_;say$r||No \$\endgroup\$ – tobyink Jan 4 '14 at 23:39
  • 1
    \$\begingroup\$ I suppose ++ might count as addition. This is the same length: $_=<>;chomp;$s=1;($s*=2)==$_&&($r=Yes)while$s<$_;say$r||No \$\endgroup\$ – tobyink Jan 4 '14 at 23:47
0
\$\begingroup\$

Excel 19 bytes

=MOD(LOG(A1,2),2)=0

Nothing magical or tricky. Input is from cell A1. Returns TRUE or FALSE.

\$\endgroup\$
0
\$\begingroup\$

AutoHotkey 33 bytes

a=%1%
While a>1
a:=a/2
Send % a=1

Input parameters are assigned to variables 1, then 2, etc. It means you have to assign it to another variable before using it in math because AHK will think you mean the number 1 and not the variable 1. It would be slightly shorter still but AHK treats a/=2 as a a floor divide by 2 instead of just a divide by 2 if and only if it is the left-most assignment operation on a line. I'm sure it has its reasons.


This is, surprisingly, slightly shorter than the method that uses log():

a=%1%
Send % Mod(Log(a)/Log(2),2)=0
\$\endgroup\$
0
\$\begingroup\$

Haskell, 41 bytes

f n=mod(2^n)n<1
main=interact$show.f.read

Alternative:

main=interact$show.(<1).(mod=<<(2^)).read

Try it online!


43 bytes

main=interact$show.(1==).until(<2)(/2).read

Try it online. Halves x until it's less than 2, and checks if the result is 1.

\$\endgroup\$
-1
\$\begingroup\$

Easiest way: An unsigned number is a power of 2 if only 1 BIT is set: if (!(n&(n-1))). Subtracting 1 inverts all BITs. if n=10000000b (80h/128), n&01111111b=0

if (!(n&(n-1)))
  printf("%d is a power of 2", n);
else
 printf("%d is NOT a power of 2", n);

// to get specific power of 2, search from
// right to left for 1st 0 BIT

int pow2(unsigned n) { // is power of 2?
  if (n&(n-1) || n<2) // if not, return 0
  return 0;
  n--;
  for (int i=1; n&1<<i; i++);
  return i; // 2^i
}
\$\endgroup\$
  • 1
    \$\begingroup\$ using + or - is not allowed... \$\endgroup\$ – ratchet freak Jan 4 '14 at 22:42
  • 1
    \$\begingroup\$ did you even read the spec? you are using both + and - \$\endgroup\$ – Tim Seguine Jan 4 '14 at 22:52
-1
\$\begingroup\$

Python:

print 0==int(bin(input())[3:] or 1)
\$\endgroup\$
  • 2
    \$\begingroup\$ 10 is a false positive \$\endgroup\$ – Joachim Isaksson Jan 4 '14 at 19:57
-1
\$\begingroup\$

Python 3.6, 30 bytes

from enum import _power_of_two

... there's a builtin lurking in the stdlib ...

\$\endgroup\$
-2
\$\begingroup\$

Basic BASH command

# assume integer is stored in variable QUERY
[ "$[(${QUERY}/2)*2]" = "$QUERY" ] && echo "Is even" || echo "Is odd"

Takes advantage of how the shell does integer division, and drops any remainder.

weylin

\$\endgroup\$
  • 2
    \$\begingroup\$ "Power of 2" is not the same thing as "even". \$\endgroup\$ – Ilmari Karonen Jan 7 '14 at 1:34
-3
\$\begingroup\$
#include <stdio.h>

int main(){
    int i=16;
    if(i&(i-1))
        printf("No\n");
    else
        printf("Yes\n");
    return 0;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ You use - operation. \$\endgroup\$ – gthacoder Jan 4 '14 at 14:46
  • 3
    \$\begingroup\$ First of all, you use the - operator, that's not allowed. Also, the question is a code-golf, so the intension is that you code should be as small as possible, so remove whitespace if possible, and include the language and the character count in your answer. \$\endgroup\$ – ProgramFOX Jan 4 '14 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.