473,396 Members | 2,039 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.

Check if power of 2 algorithm ???

Hi All,

I have a question about a formula I found online at
http://graphics.stanford.edu/~seande...mineIfPowerOf2.

To determine if an integer is a power of 2, the author states that:

f = (v & (v -1)) == 0;

I am very proficient with scripting techniques using PHP so I
understand what we're trying to accomplish here.

However, I can't seem to figure out how to make the example work... :-(

Take, for example, 16.

f = (16 & (16 - 1)) == 0;
f = (16 & (15)) == 0;

The test is always false????????

I think I am not understanding the steps completely...

For reference, in PHP, there is no operator &. The logical AND operator
is &&.

In PHP, the same formula would be written as:

if((v && (v-1)) == 0){
return true;
}

Can anyone explain this to me? I would greatly appreciate any help.

Thanks in advance,

Tim Bellefy

May 19 '06 #1
7 8684
Emmm, you know you need to prefix variable names with dollar signs,
right?

May 19 '06 #2
Sorry...

if(($v && ($v-1)) == 0){
return true;
}

OR

$r = ($v && ($v) == 0 ? true : false);

May 19 '06 #3
Well I fell like a moron...

The & is a bitwise operator and the function works just fine...

I forgot all about those littly guys.

Sorry for my rookie mistake.

May 19 '06 #4
Rik
be*****@gmail.com wrote:
Well I fell like a moron...

The & is a bitwise operator and the function works just fine...

I forgot all about those littly guys.

Sorry for my rookie mistake.


Hehe, and it's even in the url: "bithacks" :-)

Grtz,
--
Rik Wasmus
May 19 '06 #5
be*****@gmail.com wrote:
$r = ($v && ($v) == 0 ? true : false);


Better:

$r = (($v & ($v-1)) == 0);

NEVER code '?true:false' (or the opposite).

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

May 21 '06 #6
Toby Inkster wrote:
be*****@gmail.com wrote:
$r = ($v && ($v) == 0 ? true : false);


Better:

$r = (($v & ($v-1)) == 0);

NEVER code '?true:false' (or the opposite).

This makes 0 a power of two...

Try:
$r = !($v & ($v-1)) && $v;

-david-

May 21 '06 #7
"David Haynes" <da***********@sympatico.ca> wrote in message
news:_D*****************@fe82.usenetserver.com...
Toby Inkster wrote:
be*****@gmail.com wrote:
$r = ($v && ($v) == 0 ? true : false);


Better:

$r = (($v & ($v-1)) == 0);

NEVER code '?true:false' (or the opposite).

This makes 0 a power of two...

Try:
$r = !($v & ($v-1)) && $v;


Since it also fails for negative numbers, you might as well include that
case:

$r = !($v & ($v-1)) && ($v > 0);

- Dana
May 21 '06 #8

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

Similar topics

4
by: welie | last post by:
I have a problem canceling a check box update when placing a check in it. Checkbox is not bound. Here is what happens. User clicks a check box. In the BeforeUpdate method of the control, if...
2
by: Travis.Box | last post by:
I have an MS Access userform with 16 Check Boxes. Each of the checkboxes has a different option value, which coincides with the Check Box name (eg. cb01.OptionValue = 1). At the bottom of the...
18
by: Zero | last post by:
Hi, I am calculating an integer to the pwer of a large integer, e.g. 2^5000. It turns out that the result I always get is zero. I am sure that the result is too large to store in such type...
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: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
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 ...
22
by: Yanb | last post by:
Hi, I'm new to c, so please excuse, if this is silly ;-) I made a C function, which takes IP adress from string and converts to unsigned long int. Everything works, but I found that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...

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.