delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2005/05/25/09:16:38

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Message-ID: <42947807.9DDCCD03@yahoo.com>
From: CBFalconer <cbfalconer AT yahoo DOT com>
Organization: Ched Research http://cbfalconer.home.att.net
X-Mailer: Mozilla 4.75 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: to check given no. is power of 2
References: <1116997063 DOT 905915 DOT 71670 AT z14g2000cwz DOT googlegroups DOT com>
Lines: 23
Date: Wed, 25 May 2005 13:15:51 GMT
NNTP-Posting-Host: 12.76.140.63
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc05-news.ops.worldnet.att.net 1117026951 12.76.140.63 (Wed, 25 May 2005 13:15:51 GMT)
NNTP-Posting-Date: Wed, 25 May 2005 13:15:51 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

aveo wrote:
> 
> i need a C code that checks given no. is power of 2 or not without
> checking any condition.

int qpwrof2(unsigned long n)
{
   return (n && (!(n & (n-1))));
} /* untested */

assuming n == 0 is not considered a power of 2.  Function names
beginning with 'is' are reserved, so I use q for query.  The above
should handle values up to 32 bits and return 0 or 1 only.

-- 
Some useful references about C:
  <http://www.ungerhu.com/jxh/clc.welcome.txt>
  <http://www.eskimo.com/~scs/C-faq/top.html>
  <http://benpfaff.org/writings/clc/off-topic.html>
  <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
  <http://www.dinkumware.com/refxc.html>           (C-library}
  <http://gcc.gnu.org/onlinedocs/>                  (GNU docs)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019