ispowerof2.m

来自「quadtree implementation in matlab」· M 代码 · 共 30 行

M
30
字号
% % check if given number 'n'is power of 2.
% % return 1 if given number 'n'is power of 2, otherwise return 0

function flag=ispowerof2(n)

flag=0;

table=[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,...
    1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912];

if(n>table(end) || n<1)
    error('Out out range, can not check either this value is power of 2 or not')
end

if( length(find(table == n)>0) )
    flag=1;
end

% % -------------------------------------------------------------------------
% % This program or any other program(s) supplied with it does not provide any
% % warranty direct or implied. This program is free to use/share for
% % non-commercial purpose only, for any other usage contact with author.
% % Kindly reference author.
% % Thanking you.
% % @ Copyright M Khan
% % Email: mak2000sw@yahoo.com
% %        mak2000@GameBox.net 
% % http://www.geocities.com/mak2000sw

    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?