bool IsPowerOfTwo (int value)
{
return (value & -value) == value;
}
Checking whether a number is a power of 2
#1
Posted 25 August 2004 - 09:08 PM
#2
Posted 25 August 2004 - 09:10 PM
#3
Posted 25 August 2004 - 09:18 PM
-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....
#4
Posted 25 August 2004 - 09:34 PM
#5
Posted 25 August 2004 - 10:41 PM
bool IsPowerOf2( int value )
{
return ! (value & ( value - 1 ) );
}
- Me blog
#6
Posted 26 August 2004 - 12:33 PM
anubis said:
lets see if we find some bether solution to it.. would be nice..
-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....
#7
Posted 30 October 2004 - 09:48 AM
#8
Posted 30 January 2006 - 12:59 PM
john said:
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
#9
Posted 30 January 2006 - 01:01 PM
need some thing like return (value != 0) && (value != 1) && ( ( (value-1) & value ) == 0 );
but still need to modify
#10
Posted 30 January 2006 - 01:41 PM
thakurna said:
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:
bool IsPowerOfTwo(int value)
{
return value ? ((value & -value) == value) : false;
}
-
Currently working on: the 3D engine for Tomb Raider.
#11
Posted 30 January 2006 - 06:16 PM
Alex
#12
Posted 30 January 2006 - 06:31 PM
#13
Posted 31 January 2006 - 08:11 AM
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 "+".
#14
Posted 31 January 2006 - 08:29 AM
#15
Posted 31 January 2006 - 10:50 AM
bool IsPowerOfTwo (int value)
{
return (value & (~value+1)) == value; //~value+1 equals a two's complement -value
}
-
Currently working on: the 3D engine for Tomb Raider.
#16
Posted 31 January 2006 - 12:40 PM
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
#17
Posted 31 January 2006 - 12:54 PM
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.
#18
Posted 31 January 2006 - 01:05 PM
#19
Posted 31 January 2006 - 01:16 PM
Alex said:
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.
Alex said:
The compiler only care about how your code should be interpreted vs the Standard.
It doens't read into your mind yet ;)
#20
Posted 31 January 2006 - 01:49 PM
Unfortunately for endianness and how signed integers are represented, there is no such thing.
-
Currently working on: the 3D engine for Tomb Raider.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users













