473,395 Members | 1,870 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,395 software developers and data experts.

how to find a number whether its a power of 2 or not

MJ
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

Thanks in advance
Regards
Mayur...

Nov 14 '05 #1
11 8433
In article <11**********************@g14g2000cwa.googlegroups .com>,
MJ <ma********@gmail.com> wrote:
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


This question seems to get asked every couple of weeks.

Consider this: if X is a power of 2, then (X-1) differs from X
in all the bit positions (other than the leading 0's).
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler
Nov 14 '05 #2
> X = 2 power n
I need to find it using a Single C statement whether its a power of 2
or not


http://www.catch22.net/source/snippets.asp

Top of the page

--
Martijn
http://www.sereneconcepts.nl
Nov 14 '05 #3
In article <11**********************@g14g2000cwa.googlegroups .com>,
MJ <ma********@gmail.com> wrote:
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


The answer is "Yes".

(In your problem statement, you state that X is a power of two)

Nov 14 '05 #4
"MJ" <ma********@gmail.com> wrote:
I need to find it using a Single C statement whether its a power of 2
or not


If you need to use a single statement, it's not a real problem, it's
homework. So consider _why_ you've been given this homework... would
that be to learn something about copying code from a newsgroup? I think
not.

Richard
Nov 14 '05 #5
unsigned int is_power_of_2(unsigned int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}

/* Gregory Pietsch */

Nov 14 '05 #6
MJ wrote:

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

int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1) && n % 1 == 1;
}

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

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

--
pete
Nov 14 '05 #7
pete wrote:
int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1) && n % 1 == 1;
}


For what unsigned n is n % 1 == 1? ;)

--
Peter

Nov 14 '05 #8
Peter Nilsson wrote:

pete wrote:
int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1) && n % 1 == 1;
}


For what unsigned n is n % 1 == 1? ;)


It's part of the sequence:
return !(n & n - 1) && n % 1 == 1
return !(n & n - 1) && n % 3 == 1
return !(n & n - 1) && n % 7 == 1

--
pete
Nov 14 '05 #9
pete wrote:

Peter Nilsson wrote:

pete wrote:
int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1) && n % 1 == 1;
}


For what unsigned n is n % 1 == 1? ;)


It's part of the sequence:
return !(n & n - 1) && n % 1 == 1
return !(n & n - 1) && n % 3 == 1
return !(n & n - 1) && n % 7 == 1


However, it seems to be completely wrong. Thank you.

int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1);
}

--
pete
Nov 14 '05 #10
In article <42***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
pete wrote:

Peter Nilsson wrote:

pete wrote:
> int n_is_Power_of_two(unsigned n)
> {
> return !(n & n - 1) && n % 1 == 1;
> }

For what unsigned n is n % 1 == 1? ;)


It's part of the sequence:
return !(n & n - 1) && n % 1 == 1
return !(n & n - 1) && n % 3 == 1
return !(n & n - 1) && n % 7 == 1


However, it seems to be completely wrong. Thank you.

int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1);
}


Is 0 a power of two, then?

- jonathan
Nov 14 '05 #11
Jonathan Adams wrote:

In article <42***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
pete wrote:

Peter Nilsson wrote:
>
> pete wrote:
> > int n_is_Power_of_two(unsigned n)
> > {
> > return !(n & n - 1) && n % 1 == 1;
> > }
>
> For what unsigned n is n % 1 == 1? ;)

It's part of the sequence:
return !(n & n - 1) && n % 1 == 1
return !(n & n - 1) && n % 3 == 1
return !(n & n - 1) && n % 7 == 1


However, it seems to be completely wrong. Thank you.

int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1);
}


Is 0 a power of two, then?


Not really. Thank you.

int n_is_Power_of_two(unsigned n)
{
return !(n & n - 1) && n != 0;
}

--
pete
Nov 14 '05 #12

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

Similar topics

26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
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.
20
by: ravi | last post by:
Give a one-line C expression to test whether a number is a power of 2.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.