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

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Received-SPF: none (free.tvtel.pt: domain of cosmos AT tvtel DOT pt does not designate permitted sender hosts)
Message-ID: <00e301c56166$b7edca60$b40a6652@rui>
From: "cosmos" <cosmos AT tvtel DOT pt>
To: <djgpp AT delorie DOT com>
References: <1116997063 DOT 905915 DOT 71670 AT z14g2000cwz DOT googlegroups DOT com> <d71i5h$t6h$1 AT nntp DOT fujitsu-siemens DOT com> <3E3le.7676$M36 DOT 381 AT newsread1 DOT news DOT atl DOT earthlink DOT net> <00bd01c5615f$ce6a7d80$b40a6652 AT rui>
Subject: Re: to check given no. is power of 2 ( formatted text...)
Date: Wed, 25 May 2005 21:16:59 +0100
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
X-Virus-Scanned: ClamAV version 0.84, clamav-milter version 0.84e on free.tvtel.pt
X-Virus-Status: Clean
Reply-To: djgpp AT delorie DOT com

----- Original Message -----
From: "cosmos" <cosmos AT tvtel DOT pt>
To: <djgpp AT delorie DOT com>
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 <stdio.h>
> #include <math.h>
>   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" <mambuhl AT earthlink DOT net>
> Newsgroups: comp.os.msdos.djgpp
> To: <djgpp AT delorie DOT com>
> Sent: Wednesday, May 25, 2005 7:39 PM
> Subject: Re: to check given no. is power of 2
>
>
> > 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