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 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)
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
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.
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)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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.
...
|
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...
|
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...
|
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...
|
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;
|
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...
|
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...
|
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
...
|
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)
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |