X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=MIsVmIEWpkSHv7vRcD0uVIpsT3f27e1CJSB0l3l8I/k3c88L90iFunjo3PZ9aWfVrJgG12fIZbsOJ/DTVvs+5dHkTKIt62XUqApfOrln1KzTY/uOVPonkeccdumE4YxFAN+ugF0GmMKsPrGxO5YftTPdxjWcEqaooYXueDv8E2g= Message-ID: <260f63050525135825d3128c@mail.gmail.com> Date: Wed, 25 May 2005 15:58:48 -0500 From: Larry Ziegenbein To: djgpp AT delorie DOT com Subject: Re: to check given no. is power of 2 ( formatted text...) In-Reply-To: <00e301c56166$b7edca60$b40a6652@rui> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Disposition: inline References: <1116997063 DOT 905915 DOT 71670 AT z14g2000cwz DOT googlegroups DOT com> <3E3le.7676$M36 DOT 381 AT newsread1 DOT news DOT atl DOT earthlink DOT net> <00bd01c5615f$ce6a7d80$b40a6652 AT rui> <00e301c56166$b7edca60$b40a6652 AT rui> Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id j4PKwpNe005573 Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk Hi Rui, I think when you treat a double as an int, for example comparing fracpart to 0 in this line -- if (fracpart == 0) return 1; you will most often be disappointed. Doubles will generally have a bit of imprecision in the last few placeholders and throw you off unless you plan for it in advance. Give it a try and step through this code so you understand. ;-) You could round the result to the appropriate number of digits and recast it as an int before the compare maybe to make it work, but the other solutions posted are pretty nice looking to me!! Anyway, this code generally won't work, and it is dangerous to do things like this, at least for newbies like me, I avoid this like the plague. But I screw up in other places to make up for it. happy hacking, Larry On 5/25/05, cosmos wrote: > > ----- Original Message ----- > From: "cosmos" > To: > Sent: Wednesday, May 25, 2005 8:27 PM > Subject: Re: to check given no. is power of 2 > > > > Hi, > > > > Mathematically, a given number "N" is a power of 2 if "log(N) / log(2)" is > > an integer, right? So: > > > > #include > > #include > > double param, fractpart, intpart; > > > > param = log(N) / log(2); > > fractpart = modf (param , &intpart); > > if (fracpart == 0) return 1; else return 0; > > > Regards, > > Rui Fernandes > > > > > > ----- Original Message ----- > > From: "Martin Ambuhl" > > Newsgroups: comp.os.msdos.djgpp > > To: > > Sent: Wednesday, May 25, 2005 7:39 PM > > Subject: Re: to check given no. is power of 2 > > > > > > > Gerd Termathe wrote: > > > > "aveo" 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))); > > > } > > > > > > > > > >