473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

to test whether a number is a power of 2

Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]

Jul 8 '07 #1
20 10669
ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
Do your own homework.

Brian
Jul 8 '07 #2
ravi wrote:
>
Give a one-line C expression to test whether a number is a power
of 2. [No loops allowed]
For unsigned integers:

if (!((n - 1) & n)) puts("n is power of 2");

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jul 8 '07 #3
ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
}
--
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
"Hamas: Islam will conquer US and Britain."
-- http://www.pmw.org.il/LatestBulletins.htm#b220606
Jul 8 '07 #4
Erik de Castro Lopo said:
ravi wrote:
>Give a one-line C expression to test whether a number is a power of
2.
[No loops allowed]

static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
}
Having proved willing to do homework for free, you and Chuck might want
to brace yourselves for the rush.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 8 '07 #5
On Sun, 08 Jul 2007 17:23:03 +1000, Erik de Castro Lopo wrote:
ravi wrote:
>Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]

static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
What if x <= 0 (in which case x cannot be a power of two)?
}
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 8 '07 #6
Richard Heathfield wrote:
Erik de Castro Lopo said:
>ravi wrote:
>>Give a one-line C expression to test whether a number is a power
of 2.

static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;
}

Having proved willing to do homework for free, you and Chuck might want
to brace yourselves for the rush.
I like my version better :-) In all seriousness, this is not such
a simple thing, so I doubt it was homework. If he hadn't used such
rude terminology I would be virtually sure it is not.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 8 '07 #7
CBFalconer <cb********@yahoo.comwrote:

[OP wrote:]
>Give a one-line C expression to test whether a number is a power
of 2.
I like my version better :-) In all seriousness, this is not such
a simple thing, so I doubt it was homework. If he hadn't used such
rude terminology I would be virtually sure it is not.
Given the number of ridiculous questions that are routinely asked here
that clearly *are* homework, I'm surprised you find it unlikely that
the above question is not homework. Why else would there be
constraints such as "one line" or "no loops allowed" (snipped from
above context)? I personally find it highly unlikely that anyone
asking the question seriously would phrase it so tersely.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jul 9 '07 #8
On Jul 8, 5:54 am, Army1987 <army1...@NOSPAM.itwrote:
On Sun, 08 Jul 2007 17:23:03 +1000, Erik de Castro Lopo wrote:
ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
static inline int
is_power_of_2 (int x)
{
return ((x & (x - 1)) == 0) ;

What if x <= 0 (in which case x cannot be a power of two)?
Oh for crying out loud! The other answers had the OP perfectly set up
with the wrong answer (brilliant coordination, BTW, guys -- all of you
getting it simultaneously wrong in exactly the same way). Now the OP
can figure it out completely correctly without any research himself.
Good job.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 9 '07 #9
On 8 Jul, 23:37, Harald van D k <true...@gmail.comwrote:
Flash Gordon wrote:
Eric Sosman wrote, On 08/07/07 13:27:
ravi wrote:
On Jul 8, 10:22 am, Martin Ambuhl <mamb...@earthlink.netwrote:
ravi wrote:
>>>Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
<snip>
[...] Had you enough sense you would have instantly known the answer
was:
int powerof2( double num ) { return 1; }
After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.

What about zeroes, infinities, and NaNs? I would think it should return 0
for those
ah, but the spec said test a *number* and I don't think infinity is a
number and as for NaN...

So it should indicate some sort of domain error or assert on a debug
build.
--
Nick Keighley

Jul 10 '07 #10
In article <pi************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>Eric Sosman wrote, On 08/07/07 13:27:
>ravi wrote:
>>On Jul 8, 10:22 am, Martin Ambuhl <mamb...@earthlink.netwrote:
ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
>Probably. Had you enough sense you would have instantly known the answer
was:
>int powerof2( double num ) { return 1; }
>After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.
But the OP did specify that the expression be one-line. Even if we
were to restrict our use of "number" to "the numbers representable
in C", since C does not have any run-time typing (RTT), I don't think
it would be possible to write a one-line expression that would take
an arbitrary number (of -any- of the valid C numeric types) as parameter
and return any meaningful information about it. If we could at least
receive the number as a void* and a type indicator, we could unionize
and ?: the problem to death.
--
Programming is what happens while you're busy making other plans.
Jul 10 '07 #11
Walter Roberson wrote:
>
In article <pi************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
Eric Sosman wrote, On 08/07/07 13:27:
ravi wrote:
On Jul 8, 10:22 am, Martin Ambuhl <mamb...@earthlink.netwrote:
ravi wrote:
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
[...]
int powerof2( double num ) { return 1; }
After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.

But the OP did specify that the expression be one-line. Even if we
were to restrict our use of "number" to "the numbers representable
in C", since C does not have any run-time typing (RTT), I don't think
it would be possible to write a one-line expression that would take
an arbitrary number (of -any- of the valid C numeric types) as parameter
and return any meaningful information about it. If we could at least
receive the number as a void* and a type indicator, we could unionize
and ?: the problem to death.
Why not a macro?

#define POWEROF2(value) (1)

However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jul 10 '07 #12
In article <pi************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>Had you enough sense you would have instantly known the answer
was:
>int powerof2( double num ) { return 1; }
>After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.
Hey, I just realized that the specification required that the
expression -test- that the number was a power of 2, but the
specification didn't require that the expression evaluated to
anything in particular either way -- or even that the expression
got the test right in any or all cases.

For example, we commonly speak of "testing whether a number is prime"
and in a number of cases are satisfied if the test is looking for
"obvious" non-primes, something fast as a filter to keep us from
having to use slower but more complete algorithms.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jul 10 '07 #13
Kenneth Brody wrote, On 10/07/07 18:52:
Walter Roberson wrote:
>In article <pi************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>>Eric Sosman wrote, On 08/07/07 13:27:
ravi wrote:
On Jul 8, 10:22 am, Martin Ambuhl <mamb...@earthlink.netwrote:
>ravi wrote:
>>Give a one-line C expression to test whether a number is a power of 2.
>> [No loops allowed]
[...]
>>int powerof2( double num ) { return 1; }
After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.
But the OP did specify that the expression be one-line. Even if we
were to restrict our use of "number" to "the numbers representable
in C", since C does not have any run-time typing (RTT), I don't think
it would be possible to write a one-line expression that would take
an arbitrary number (of -any- of the valid C numeric types) as parameter
and return any meaningful information about it. If we could at least
receive the number as a void* and a type indicator, we could unionize
and ?: the problem to death.

Why not a macro?

#define POWEROF2(value) (1)

However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)
You could check for 0 then.
#define POWEROF2(value) ((value)!=0)

Hard to do something that will cope with inf and NaN for both complex
and doubles in C99, but we could lobby for adding type-generic versions
of isnan and isinf to C0x to solve this problem.
--
Flash Gordon
Jul 10 '07 #14
Walter Roberson wrote, On 10/07/07 19:09:
In article <pi************@news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>Had you enough sense you would have instantly known the answer
was:
>int powerof2( double num ) { return 1; }
>After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.

Hey, I just realized that the specification required that the
expression -test- that the number was a power of 2,
See elsethread where I added some tests. Specifically for inf, NaN and 0.
but the
specification didn't require that the expression evaluated to
anything in particular either way -- or even that the expression
got the test right in any or all cases.
OK, replace the 1 by (num!=atan(num))
For example, we commonly speak of "testing whether a number is prime"
and in a number of cases are satisfied if the test is looking for
"obvious" non-primes, something fast as a filter to keep us from
having to use slower but more complete algorithms.
Oh, you were thinking of the less accurate test being faster. <shrug>
Well, that was not in the original spec either.
--
Flash Gordon
Jul 10 '07 #15
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Kenneth Brody wrote, On 10/07/07 18:52:
[...]
>Why not a macro?
#define POWEROF2(value) (1)
However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)

You could check for 0 then.
#define POWEROF2(value) ((value)!=0)
Or, more simply:

#define POWEROF2(value) (value)

Both forms have the interesting property of treating non-null pointers
as powers of 2.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 10 '07 #16
ravi wrote:
>
Give a one-line C expression to test whether a number is a power of 2.
[No loops allowed]
int n_is_Power_of_two(long unsigned n)
{
return (n & n - 1) == 0 && n != 0;
}

int n_is_Power_of_four(long unsigned n)
{
return (n & n - 1) == 0 && n % 3 == 1;
}

int n_is_Power_of_eight(long unsigned n)
{
return (n & n - 1) == 0 && n % 7 == 1;
}

--
pete
Jul 10 '07 #17
Keith Thompson wrote, On 10/07/07 21:49:
Flash Gordon <sp**@flash-gordon.me.ukwrites:
>Kenneth Brody wrote, On 10/07/07 18:52:
[...]
>>Why not a macro?
#define POWEROF2(value) (1)
However, can zero be expressed as a power of 2? (I suppose raising 2
to the power of negative infinity may qualify, but I'm not certain.)
You could check for 0 then.
#define POWEROF2(value) ((value)!=0)

Or, more simply:

#define POWEROF2(value) (value)

Both forms have the interesting property of treating non-null pointers
as powers of 2.
Perhaps that will help those people who want to do strange things
because the believe (incorrectly) that pointers are just integers then :-)
--
Flash Gordon
Jul 10 '07 #18
Christopher Benson-Manica wrote:
CBFalconer <cb********@yahoo.comwrote:
[OP wrote:]
>>Give a one-line C expression to test whether a number is a power
of 2.
>I like my version better :-) In all seriousness, this is not such
a simple thing, so I doubt it was homework. If he hadn't used such
rude terminology I would be virtually sure it is not.

Given the number of ridiculous questions that are routinely asked here
that clearly *are* homework, I'm surprised you find it unlikely that
the above question is not homework. Why else would there be
constraints such as "one line" or "no loops allowed" (snipped from
above context)? I personally find it highly unlikely that anyone
asking the question seriously would phrase it so tersely.
You have a definite point, and are probably right.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 11 '07 #19
On Mon, 09 Jul 2007 08:13:20 +0100, Flash Gordon wrote:
Harald van Dijk wrote, On 08/07/07 23:37:
>Flash Gordon wrote:
>>Probably. Had you enough sense you would have instantly known the answer
was:

int powerof2( double num ) { return 1; }

After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.

What about zeroes, infinities, and NaNs? I would think it should return 0
for those.

Zero is easy to deal with, infinities could be argued as being powers of
2, [snip]
The argument about infinities also applies to zero. The difference
is just the sign of the exponent.

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 13 '07 #20
Army1987 wrote, On 13/07/07 13:31:
On Mon, 09 Jul 2007 08:13:20 +0100, Flash Gordon wrote:
>Harald van Dijk wrote, On 08/07/07 23:37:
>>Flash Gordon wrote:
Probably. Had you enough sense you would have instantly known the answer
was:

int powerof2( double num ) { return 1; }

After all, the OP did not specify integer power of 2, or even real power
of 2, and I think if you allow complex powers, all numbers will qualify.
What about zeroes, infinities, and NaNs? I would think it should return 0
for those.
Zero is easy to deal with, infinities could be argued as being powers of
2, [snip]
The argument about infinities also applies to zero. The difference
is just the sign of the exponent.
I didn't say it could not, I just said it could be dealt with easily.

I also did not think about it ;-)
--
Flash Gordon
Jul 13 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Matt | last post by:
Give a one-line C expression to test whether a number is a power of 2. No loops allowed.
11
by: MJ | last post by:
Hi I have question about the C. I have a number X which is power of two X = 2 power n I need to find it using a Single C statement whether its a power of 2 or not I know that a number with...
10
by: David T. Ashley | last post by:
What is the most economical test in 'C' for "integer is a power of 2"? For example, something better than: void is_2_pow(int arg) { return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.