hymn

忽有故人心头过,回首山河已是秋。

  menu
132 文章
0 浏览
1 当前访客
ღゝ◡╹)ノ❤️

判断一个数是否是 2 的整数次幂次

image.png

/**
 * @author: daixyhymn
 * @description:
 * @date: 2020/11/6 16:56
 */
public class isPowerOf2 {
  /**
   * 判断一个数是否使 2 的倍数
   * 规律
   *      如果一个数 a 是 2 的倍数, (a & a -1) == 0
   * @param a
   * @return
   */
  public static boolean isPowerOf2(int a){
    return (a & a -1) == 0;
  }

  public static void main(String[] args) {
    System.out.println(isPowerOf2(16));
  }
}


标题:判断一个数是否是 2 的整数次幂次
作者:hymn
地址:https://dxyhymn.com/articles/2020/11/06/1604653300687.html