Diferents approaches to get next highest power of 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
////////////////////////////
var powerOf2:int=1;
var val:int=456;
while ( powerOf2 <val )
{
    trace(powerOf2 <<= 1);
}

////////////////////////////
function nextPowerOfTwo( value_ : int ):int
{
    value_--;
    value_ = (value_>> 1) | value_;
    value_ = (value_>> 2) | value_;
    value_ = (value_>> 4) | value_;
    value_ = (value_>> 8) | value_;
    value_ = (value_>> 16) | value_;
    value_++;
    return value_;
}

////////////////////////////
function nextPowerOfTwo( value_ : int ):int
{
    return int(Math.pow(2,Math.ceil(Math.log(value_) / Math.log(2))));
}

From: http://www.blog.lessrain.com/flash-more-efficient-blur-filter-values/

Comment are closed.