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

power of 2

is it possible to write a code using bitwise operators for checking
whether a number is power of 2 or in general power of n
(n=2,3,4,...)??

Thanks a lot.

Regards,
Raghu

Sep 14 '06 #1
6 8230
raghu said:
is it possible to write a code using bitwise operators for checking
whether a number is power of 2
Yes.
or in general power of n
(n=2,3,4,...)??
Yes.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 14 '06 #2
raghu posted:
is it possible to write a code using bitwise operators for checking
whether a number is power of 2

There's a recent thread which counts the number of set bits in an integer
which would work for you, e.g.:

#define IS_POW_TWO(x) (1==QUANT_BITS((x)))

or in general power of n
(n=2,3,4,...)??

#define IS_POW_X(x,pow) (!((x)%(pow)))

--

Frederick Gotham
Sep 14 '06 #3

Frederick Gotham wrote:
raghu posted:
is it possible to write a code using bitwise operators for checking whether
(snip)
or in general power of n
(n=2,3,4,...)??


#define IS_POW_X(x,pow) (!((x)%(pow)))
No, this will check whether x is a multiple of pow. For example,
!(6%2) is true even though 6 is certainly not a power of 2.

Sep 14 '06 #4
Frederick Gotham said:
raghu posted:
>is it possible to write a code using bitwise operators for checking
whether a number is power of 2


There's a recent thread which counts the number of set bits in an integer
which would work for you, e.g.:

#define IS_POW_TWO(x) (1==QUANT_BITS((x)))
There's an easier way.
> or in general power of n
(n=2,3,4,...)??


#define IS_POW_X(x,pow) (!((x)%(pow)))
It doesn't use bitwise operators and doesn't check whether a number is a
power of n. Oops.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 14 '06 #5
raghu wrote:
>
is it possible to write a code using bitwise operators for checking
whether a number is power of 2 or in general power of n
(n=2,3,4,...)??
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
Sep 14 '06 #6
In general, for bit twiddling:
http://graphics.stanford.edu/~seander/bithacks.html

Which has this (for instance):
http://graphics.stanford.edu/~seande...mineIfPowerOf2

Which is Pete's solution.

Another good source for interesting things like this is hackmem:
http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html

Sep 15 '06 #7

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

Similar topics

6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
12
by: Roman Töngi | last post by:
I just want to raise a number to a power. e.g.: 16^2 The pow()-function of cmath seems to be not enough. I've read about valarray. Is it really so difficult? Can you give me an example. ...
0
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
0
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
1
by: Richard Fennell | last post by:
Am trying to do ASP.NET development on my XP Prof. box logged in as a power user (not the administrator I used to be). As an admin user all is OK. But as a power user, I have made sure my...
5
by: mathon | last post by:
hi, i develeoped a recursive function for the power and it works fine double power(double x, int n) { if (n < 0) return 1/power(x, -n); else if (n == 0) return 1.0;
9
by: mathon | last post by:
hi, i already posted an entry because of this problem, unfortunately i havent solved it so far..:( I have created a recursion for the calculation of the power like this: double...
6
by: maxthebaz | last post by:
Our machines have this requirement: if power failure occurs, many important variables are to be resumed from where they were interrupted after the machine is restarted (power on in this case). In...
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
8
by: skanemupp | last post by:
how do i solve power(5,1.3)? def power(nbr, po): if po==0: return 1 if po>0: return nbr*power(nbr, po-1) if po<0: return 1/power(nbr, -1*po)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.