BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

MathUtils.isPowerOfTwo()

The MathUtils.isPowerOfTwo() function is a method in the Three.js library that checks whether a given integer is a power of two.

Syntax

THREE.MathUtils.isPowerOfTwo(value)

Parameters

  • value - An integer value to check whether it is a power of two.

Return

  • true if value is a power of two, false otherwise.

Description

The isPowerOfTwo() method is used to check whether a given integer is a power of two. This method is useful in situations where you need to perform certain operations only if the number is a power of two.

Internally, the isPowerOfTwo() method is implemented using a bitwise AND operation. A power of two can be represented as a binary number with only one bit set to one. For example, 4 (100 in binary) and 8 (1000 in binary) are powers of two. When we perform a bitwise AND between this number and that number minus one, we get 0. For example, 4 & 3 (011 in binary) is equal to 0. This is true for all powers of two.

MathUtils.isPowerOfTwo = function ( value ) {
    return ( value & ( value - 1 ) ) === 0 && value !== 0;
};

Example

let isPowerOfTwo = THREE.MathUtils.isPowerOfTwo;

console.log(isPowerOfTwo(2)); // true
console.log(isPowerOfTwo(3)); // false
console.log(isPowerOfTwo(4)); // true
console.log(isPowerOfTwo(5)); // false
console.log(isPowerOfTwo(8)); // true
console.log(isPowerOfTwo(10)); // false

Conclusion

In conclusion, the MathUtils.isPowerOfTwo() method is a built-in method in the Three.js library. This method checks whether a given integer is a power of two using a bitwise AND operation. This is useful when you need to perform some operations only if the number is a power of two.