# Check whether an integer is a power of 2 without using +,- operations [closed]

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.

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

Perl 69

$_=<>;chomp;$s=1;while($s<$_){$s*=2;$r="Yes" if $s==$_}print $r||"No"  • 58: $_=<>;chomp;++$s;($s*=2)==$_&&($r=Yes)while$s<$_;say$r||No Jan 4, 2014 at 23:39 • I suppose ++ might count as addition. This is the same length: $_=<>;chomp;$s=1;($s*=2)==$_&&($r=Yes)while$s<$_;say$r||No Jan 4, 2014 at 23:47 # Excel 19 bytes =MOD(LOG(A1,2),2)=0  Nothing magical or tricky. Input is from cell A1. Returns TRUE or FALSE. # 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  # 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.

# Python 3.6, 30 bytes

from enum import _power_of_two


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

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
}

• using + or - is not allowed... Jan 4, 2014 at 22:42
• did you even read the spec? you are using both + and - Jan 4, 2014 at 22:52

Python:

print 0==int(bin(input())[3:] or 1)

• 10 is a false positive Jan 4, 2014 at 19:57

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

• "Power of 2" is not the same thing as "even". Jan 7, 2014 at 1:34
• Nuts - you're right. Sorry for any confusion. Sep 10, 2021 at 0:07
#include <stdio.h>

int main(){
int i=16;
if(i&(i-1))
printf("No\n");
else
printf("Yes\n");
return 0;
}

• You use - operation. Jan 4, 2014 at 14:46
• 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. Jan 4, 2014 at 15:00