zl程序教程

您现在的位置是:首页 >  其它

当前栏目

判断一个数是不是2的N次方

一个 判断 是不是 次方
2023-09-14 09:07:07 时间
package com.oracle.test;

import org.testng.annotations.Test;

public class IsPowerTest {

    public boolean isTwoPower(int num) {
        return (num & (num-1)) == 0;
    }

    @Test
    void testIsPower() {
        System.out.println(isTwoPower(9));
    }
}

输出如下,
在这里插入图片描述