Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Mathf.ClosestPowerOfTwo

static function ClosestPowerOfTwo(value: int): int;
static int ClosestPowerOfTwo(int value);
static def ClosestPowerOfTwo(value as int) as int

Description

Returns the closest power of two value.

	// prints 8
	Debug.Log(Mathf.ClosestPowerOfTwo(7));
	
	// prints 16
	Debug.Log(Mathf.ClosestPowerOfTwo(19));
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        Debug.Log(Mathf.ClosestPowerOfTwo(7));
        Debug.Log(Mathf.ClosestPowerOfTwo(19));
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Example() as void:
		Debug.Log(Mathf.ClosestPowerOfTwo(7))
		Debug.Log(Mathf.ClosestPowerOfTwo(19))