473,748 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10734
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.securityfoc us.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.securityfoc us.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********@yah oo.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)gma il.com | don't, I need to know. Flames welcome.
Jul 9 '07 #8
On Jul 8, 5:54 am, Army1987 <army1...@NOSPA M.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...@earthl ink.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

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

Similar topics

17
21598
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
8455
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 power of 2 will have only one single bit as 1 and rest of the bit as 0. To check the bit I need to go through all the bits ones and check for the condition. I dont want use a loop. In that case how can I do this
10
4936
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 == 16) /* and so on */ ); }
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.