DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 08-25-2004, 03:08 PM   #1
john
Member
 
Join Date: Jan 2003
Posts: 85
Default

Code:
bool IsPowerOfTwo (int value) { return (value & -value) == value; }
john is offline   Reply With Quote
Old 08-25-2004, 03:10 PM   #2
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default

the good old trick
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline   Reply With Quote
Old 08-25-2004, 03:18 PM   #3
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

indeed. good to get the algorithms and code snippets filled with such stuff. thats what its ment for!
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 08-25-2004, 03:34 PM   #4
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default

hmm... let's hope people use the search function in this forum, unlike other places we don't want to mention
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline   Reply With Quote
Old 08-25-2004, 04:41 PM   #5
bladder
DevMaster Staff
 
bladder's Avatar
 
Join Date: Sep 2003
Location: Hell
Posts: 1,109
Default

another one

Code:
bool IsPowerOf2( int value ) { return ! (value & ( value - 1 ) ); }
___________________________________________
- TripleBuffer
- Me blog
bladder is offline   Reply With Quote
Old 08-26-2004, 06:33 AM   #6
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

Quote:
Originally Posted by anubis
hmm... let's hope people use the search function in this forum, unlike other places we don't want to mention
[snapback]9650[/snapback]
indeed...

lets see if we find some bether solution to it.. would be nice..
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 10-30-2004, 03:48 AM   #7
eyebex
New Member
 
Join Date: Oct 2004
Posts: 7
Default

For two more useful functions which round to the next bigger / next smaller power of two, see: http://www.flipcode.com/cgi-bin/fcarticles...show=4&id=64182
___________________________________________
http://threekings.tk/
eyebex is offline   Reply With Quote
Old 01-30-2006, 05:59 AM   #8
thakurna
New Member
 
Join Date: Jan 2006
Location: Pune, India
Posts: 2
Default Re: Checking whether a number is a power of 2

Quote:
Originally Posted by john
Code:
bool IsPowerOfTwo (int value) { return (value & -value) == value; }

It is not going to work for if the numbers are = 1, 0, -1....
Can give you solution in that case
thakurna is offline   Reply With Quote
Old 01-30-2006, 06:01 AM   #9
thakurna
New Member
 
Join Date: Jan 2006
Location: Pune, India
Posts: 2
Default Re: Checking whether a number is a power of 2

this solution will not work for if if the number = 1, 0, -1....
need some thing like return (value != 0) && (value != 1) && ( ( (value-1) & value ) == 0 );
but still need to modify
thakurna is offline   Reply With Quote
Old 01-30-2006, 06:41 AM   #10
.oisyn
DevMaster Staff
 
.oisyn's Avatar
 
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
Default Re: Checking whether a number is a power of 2

Quote:
Originally Posted by thakurna
It is not going to work for if the numbers are = 1, 0, -1....

Only not for zero. 1 & -1 == 1, which is correct as 1 is a power of two (2^0 to be exact). -1 & 1 is of course also 1, but now 'value' is -1 so the end result doesn't equal 'value', therefore it is not a power of two (this works for every negative value btw)

Only the zero is a special case. So it would be:
Code:
bool IsPowerOfTwo(int value) { return value ? ((value & -value) == value) : false; }
___________________________________________
C++ addict
-
Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3.
.oisyn is offline   Reply With Quote
Old 01-30-2006, 11:16 AM   #11
Alex
Valued Member
 
Join Date: Sep 2005
Location: Germany
Posts: 152
Default Re: Checking whether a number is a power of 2

Is a bitwise "&" consistent across platforms?? Just curious...

Alex
Alex is offline   Reply With Quote
Old 01-30-2006, 11:31 AM   #12
Mihail121
Senior Member
 
Mihail121's Avatar
 
Join Date: Jan 2003
Posts: 868
Default Re: Checking whether a number is a power of 2

It depends what language standards the compiler follows but for the most cases yes.
Mihail121 is offline   Reply With Quote
Old 01-31-2006, 01:11 AM   #13
eddie
Senior Member
 
Join Date: Oct 2005
Posts: 745
Default Re: Checking whether a number is a power of 2

What?

I've never heard of bitwise & not being consistent. That wouldn't be "not adhering to standards", that would be a *bug*.

Unless I'm missing something. Mihail121, do you know of a compiler that doesn't handle this?

I'd find it incredible to hear that there's a "standard" that defies normal bitwise &. It's like saying that my compiler has a different standard for "+".
___________________________________________
Kicking Dragon (dot) com - Technical Ramblings of a Game Programmer
eddie is offline   Reply With Quote
Old 01-31-2006, 01:29 AM   #14
Reedbeta
DevMaster Staff
 
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
Default Re: Checking whether a number is a power of 2

Well, bitwise & ought to work the same way everywhere, but not all machines are two's complement and so some of the formulas posted above will not always work. (Realistically though, any machine you are going to be writing code for nowadays uses two's complement for integer arithmetic.)
___________________________________________
Currently working at Sucker Punch
reedbeta.com - OpenGL demos and other projects
Luabridge - a lightweight, dependency-free C++/Lua binding library.
CD Lite - an unobtrusive, minimal CD player application for Windows.
Reedbeta is offline   Reply With Quote
Old 01-31-2006, 03:50 AM   #15
.oisyn
DevMaster Staff
 
.oisyn's Avatar
 
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
Default Re: Checking whether a number is a power of 2

Indeed, the standard requires signed ints to be either two's complement, one's complement or sign and magnitude. But that doesn't really matter, it only means we cannot use the negation operator in our test code. So either use bladder's, or this:

Code:
bool IsPowerOfTwo (int value) { return (value & (~value+1)) == value; //~value+1 equals a two's complement -value }
___________________________________________
C++ addict
-
Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3.
.oisyn is offline   Reply With Quote
Old 01-31-2006, 05:40 AM   #16
Alex
Valued Member
 
Join Date: Sep 2005
Location: Germany
Posts: 152
Default Re: Checking whether a number is a power of 2

I wonder why there is no support for "flags" that indicate what kind of system the code has been written for (little endian, pointer size, etc etc).
That way the compiler could easily "fix" the code (or rather change the output to do what we meant it to do) if the current target doesn't fit the flags. Why is it up to us to worry about all that crap?
Using the flags it should be inherently clear what the code is supposed to do..

Sorry to high jack the thread but this is probably not worth starting a new one..

Alex
Alex is offline   Reply With Quote
Old 01-31-2006, 05:54 AM   #17
tbp
Valued Member
 
Join Date: Mar 2005
Posts: 135
Default Re: Checking whether a number is a power of 2

Because C has been designed to write OS with, you're supposed to know what you're doing.
As it is the Standard is byzantine enough, it doesn't need even more assumptions.

Use the right tool for a given task.
If you want more abstraction from the hardware, use another language.
If you want less, write some asm.
tbp is offline   Reply With Quote
Old 01-31-2006, 06:05 AM   #18
Alex
Valued Member
 
Join Date: Sep 2005
Location: Germany
Posts: 152
Default Re: Checking whether a number is a power of 2

Now I'm probably not the only one who want's to write portable C++ code. Also the feature I suggest wouldn't stop anyone from writing OS code..it's less a language thing, more of a compiler option. There is no good reason I can see to have interpretations of my code that I have no control over but which alter the logic.

Last edited by Alex : 01-31-2006 at 06:11 AM.
Alex is offline   Reply With Quote
Old 01-31-2006, 06:16 AM   #19
tbp
Valued Member
 
Join Date: Mar 2005
Posts: 135
Default Re: Checking whether a number is a power of 2

Quote:
Originally Posted by Alex
it's less a language thing, more of a compiler option.
If it's not into the language/standard, then it's not portable across compilers.

So either you don't want to have to handle such issues and use another language that insulates you better, or rely on something like autoconf.

Quote:
Originally Posted by Alex
There is no good reason I can see to have interpretations of my code that I have no control over but which alter the logic
The only logic that is altered is the one you _think_ your code implements.
The compiler only care about how your code should be interpreted vs the Standard.
It doens't read into your mind yet

Last edited by tbp : 01-31-2006 at 06:20 AM.
tbp is offline   Reply With Quote
Old 01-31-2006, 06:49 AM   #20
.oisyn
DevMaster Staff
 
.oisyn's Avatar
 
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
Default Re: Checking whether a number is a power of 2

tbp: you are missing the point. For example, the standard states that the number of bits in a char shall be 8 or more. Fortunately, the standard also states that there shall be a CHAR_BIT constant representing the number of bits in a char. It also defines other handy constants like INT_MAX. These are all pretty implementation defined, but the point is that you have access to such constants, so you can write portable code that runs on different architectures using these constants.

Unfortunately for endianness and how signed integers are represented, there is no such thing.
___________________________________________
C++ addict
-
Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3.

Last edited by .oisyn : 01-31-2006 at 06:52 AM.
.oisyn is offline   Reply With Quote
Old 01-31-2006, 07:34 AM   #21
tbp
Valued Member
 
Join Date: Mar 2005
Posts: 135
Default Re: Checking whether a number is a power of 2

Again, portability has never been part of the set of problems C has been designed to solve.

It's just a thin abstraction layer from the metal, some souped assembler.
By design and for varied reasons like efficiency and compiler complexity.

If portability is your prime concern you shouldn't be using C (or any of its descendant) in the first place.

So, i'm not missing any point, you're just barking at the wrong tree.
tbp is offline   Reply With Quote
Old 01-31-2006, 07:59 AM   #22
Alex
Valued Member
 
Join Date: Sep 2005
Location: Germany
Posts: 152
Default Re: Checking whether a number is a power of 2

Bjarne Stroustrup lists C++ as a general-purpose language with a bias to "generic programming" (among others including OS stuff):
http://www.artima.com/cppsource/cpp0x.html
Where "Generic programming is about generalizing software components so that they can be easily reused in a wide variety of situations."
So I agree that total portability is NOT an explicit! goal of the standard.
Reuseability on the other hand means that portability is desired where it doesn't hurt the standard (or the target specific constrains).
Also note that at least I'm talking about C++, not C.
Which language(s) to use depends on the problem at hand. I doubt that a desire for portability prohibits using C++.

ALex
Alex is offline   Reply With Quote
Old 01-31-2006, 08:20 AM   #23
tbp
Valued Member
 
Join Date: Mar 2005
Posts: 135
Default Re: Checking whether a number is a power of 2

Portability isn't and never was an explicit goal of the C++ Standard for a good reason, let me cite the prophet himself:


You can talk genericity and OOness all day long, C++ is by design compatible with C.
Thusly, regarding portability they both stand side by side.
tbp is offline   Reply With Quote
Old 01-31-2006, 08:35 AM   #24
Alex
Valued Member
 
Join Date: Sep 2005
Location: Germany
Posts: 152
Default Re: Checking whether a number is a power of 2

Quote:
The C++0x improvements should be done in such a way that the resulting language is easier to learn and use. Among the rules of thumb for the committee are:
Provide stability and compatibility (with C++98, and, if possible, with C)

Being compatible with C is nice but rather optional ("if possible").

This is getting no where so this will be my last post on this...

ALex
Alex is offline   Reply With Quote
Old 01-31-2006, 08:42 AM   #25
tbp
Valued Member
 
Join Date: Mar 2005
Posts: 135
Default Re: Checking whether a number is a power of 2

Your quote is about C++0x, an extension to C++ (just like C99 was an extension of C).

If that compatibility is news to you, i'm sorry, but you should have asked yourself why you could happily (void *) everything, among other things.

Last edited by tbp : 01-31-2006 at 08:46 AM.
tbp is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 07:22 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.