Power of Two Example

=D
Hi folks, i know that there are a lot of examples :google: , but maybe it will be useful for someone
Simple example #1
[java]
public static final boolean isPowerOfTwo( final int pNumber ) throws IllegalArgumentException
{
if ( pNumber <= 0 )
throw new IllegalArgumentException( "MathHelper.isPowerOfTwo: given integer is " + pNumber );

    return( ( pNumber &amp; -pNumber ) == pNumber );
    //Method #2
    //return( ( pNumber &amp; ( pNumber - 1 ) ) == 0 );
}

[/java]

Simple example #2
[java]
public static final boolean isPowerOfTwo( final Image pImage ) throws Exception
{
final int width = pImage.getWidth( );
final int height = pImage.getHeight( );

    return( ( ( width &amp; -width ) == width &amp;&amp; ( height &amp; -height ) == height ) );
}

[/java]

There is also the FastMath class in JME

1 Like
@jmaasing said: There is also the FastMath class in JME

jmonkeyengine/FastMath.java at 3.0 · jMonkeyEngine/jmonkeyengine · GitHub


tanks, now i know