LoginSignup
10
9

More than 5 years have passed since last update.

その数字より以上の、最小の2のべき乗数を求める

Last updated at Posted at 2012-12-20

#import <stdio.h>
#import <math.h>

int pow2(int n)
{
    if(n < 0)
        return 0;
    if(n == 1)
        return 1;

    return (int)pow(2.0, (floor(log2(n - 1)) + 1.0));
}
int main(int argc, char *argv[]) {
    for(int i = 0 ; i < 1025 ; ++i)
    {
        printf("%d -> %d\n", i, pow2(i));
    }
}

GL等のテクスチャのためのリサイズの際などの用途用です
※n=1のとき0と出ていた間違いを修正しました

10
9
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9