X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f Message-ID: <42947807.9DDCCD03@yahoo.com> From: CBFalconer 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> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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: (C99) (C-library} (GNU docs)