delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2005/05/25/14:46:08

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: Martin Ambuhl <mambuhl AT earthlink DOT net>
User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)
X-Accept-Language: en-us, 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> <d71i5h$t6h$1 AT nntp DOT fujitsu-siemens DOT com>
In-Reply-To: <d71i5h$t6h$1@nntp.fujitsu-siemens.com>
Lines: 23
Message-ID: <3E3le.7676$M36.381@newsread1.news.atl.earthlink.net>
Date: Wed, 25 May 2005 18:39:59 GMT
NNTP-Posting-Host: 165.247.24.140
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread1.news.atl.earthlink.net 1117046399 165.247.24.140 (Wed, 25 May 2005 11:39:59 PDT)
NNTP-Posting-Date: Wed, 25 May 2005 11:39:59 PDT
Organization: EarthLink Inc. -- http://www.EarthLink.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Gerd Termathe wrote:
> "aveo" <rahul DOT h AT gmail DOT com> schrieb im Newsbeitrag
> news:1116997063 DOT 905915 DOT 71670 AT z14g2000cwz DOT googlegroups DOT com...
> 
>>hi all
>>i need a C code that checks given no. is power of 2 or not without
>>checking any condition.
>>
> 
> 
> int is_power_of_2 (int N)
> {
>  if ( N!=0 && (N&(N-1))==0 ) return 1; else return 0;
> }

Why an if ... else?
Why use uppercase for non-macros?
Why use signed integers?

inline unsigned is_power_of_2 (unsigned n)
{
   return (n && !(n&(n-1)));
}

- Raw text -


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