please dont rip this site

PIC Microcontoller Power of 2 Check Math Methods

Test if a value is a power of two.

a function that returns zero if INDF is a power of two, else returns non-zero.

Dmitry A. Kiryashov [nzews at MAIL.RU] says:

        decf    INDF,F  ;0/1/others -> Z=0/Z=1/Z=0
        incfsz  INDF,W  ;0?
        andwf   INDF,W

Was =0 returning Z=0

Was !=0 and power of 2 returning Z=1

Was !=0 and not a power of 2 returning Z=0

Disadvantage: INDF is decremented in this case.

Wouter adds:

the formula is
( x - 1 ) & ( x | 0x80 )



#include <stdio.h>

int f( int x ){
   return ( 0xFF & ( (x - 1) & ( x | 0x80 ) ) );

int ones( int x ){
   int n = 0;
   while( x > 0 ){
      if( x & 1 > 0 ){
      x = x >> 1;
   return n;

int main( void ){
   int i, j, k;
   for( i = 0; i < 256; i ++ ){
      j = f( i );
      k = ones( i );
      printf( "%2X %2X %2X %d\n", i, j, k, ( k == 1 ) == ( j == 0 ) );
   return 0;

Scott Dattalo says:

But it'd probably make more sense to just make this a macro:
is_power_of_2  MACRO  x
  decf  x,w
  bsf   x,7
  andwf x,w


Both W and the Z bit contain the result.

If you don't wish the register under test to be modified then this is probably the best solution:

is_power_of_2  MACRO  x

   decf   x,w
   movf   x,f
    andwf x,w

or this:

is_power_of_2  MACRO  x

   movlw  -1
   addwf  x,w
    andwf x,w


See also:

file: /Techref/microchip/pow2chk.htm, 2KB, , updated: 2017/4/10 15:01, local time: 2021/10/21 02:34,

 ©2021 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC Microcontoller Power of 2 Check Math Methods </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2021 contributors:
o List host: MIT, Site host, Top posters @20211021 Neil, Harold Hallikainen, Alan Pearce, RussellMc, Bob Blick, Allen Mulvey, Dwayne Reid, madscientistatlarge, Sean Breheny, Justin Richards,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!