java工具类 isPowerOfTwo,代码顺序
查看代码
package org.example;
import com.google.common.math.IntMath;
import com.google.common.math.LongMath;
import java.math.RoundingMode;
import java.util.stream.Stream;
public class GuavaTester {
public static void main(String[] args) {
short short1 = 0b0000_0001;
short short2 = 0b0000_0011;// Not Power Of Two;
short short3 = 0b0000_0111;// Not Power Of Two;
short short4 = 0b0000_1111;// Not Power Of Two;
short short5 = 0b0001_1111;// Not Power Of Two;
short short6 = 0b0011_1111;// Not Power Of Two;
short short7 = 0b0111_1111;// Not Power Of Two;
short short8 = 0b1111_1111;// Not Power Of Two;
short short9 = 0b1111_1101;// Not Power Of Two;
Stream.of(2, 4, 6, 8, 10, 16, 22, 44, 66, 10).filter(i -> {
boolean powerOfTwo = LongMath.isPowerOfTwo(i);
isPowerOfTwo(i);
return !powerOfTwo;
}).toList().forEach(i -> System.out.printf("%sb; -val:[%sb] (%4d) is not power of two!%n", interToBinaryString(i), interToBinaryString(-i), i));
Stream.generate(() -> '*').limit(5).forEach(System.out::print);
System.out.println(" Integer.SIZE = " + Integer.SIZE + " bits");
isPowerOfTwo(1);
generate2PowerNums(short1, 31).forEach(i -> System.out.printf("%sb; -val:[%sb] (%12d) is power of two?:%s %n", interToBinaryString(i), interToBinaryString(-i), i, LongMath.isPowerOfTwo(i)));
}
/**
* 2次方数据生成,从 start 开始,生成 count 个数据
*
* @param start 开始因子
* @param count 生成个数 不是 end 结束值
* @return 2次方数据生成流
*/
public static Stream<Integer> generate2PowerNums(int start, int count) {
if (count >= Integer.SIZE) {
throw new IllegalArgumentException("count must be less than Integer.SIZE:" + Integer.SIZE);
}
return Stream.iterate(start, item -> item << 1).limit(count);
}
public static String interToBinaryString(int i) {
return binaryStringBeautiful("0".repeat(Integer.numberOfLeadingZeros(i)) + Integer.toBinaryString(i), '_');
}
public static String binaryStringBeautiful(String s, char insertChar) {
return s.replaceAll("(.{4})(?!$)", "$1" + insertChar);
}
/**
* 判断 val 是否为 2 的 N 次方(幂)
*
* @param val 被测试的值
* @return 是否为 2 的 N 次方(幂)
*/
public static boolean isPowerOfTwo(int val) {
if (val <= 0) {
return false;
}
boolean isPowerOfTwo = (val & -val) == val;
if (isPowerOfTwo) {
System.out.printf("%sb; -val: %sb; (%4d) is power of two! N = %d %n", interToBinaryString(val), interToBinaryString(-val), val, IntMath.log2(val, RoundingMode.UNNECESSARY));
}
return isPowerOfTwo;
}
}
10 的幂次:
1
10
100
1000
……
2的幂次:
0000_0000_0000_0001b; 1 is power of two
0000_0000_0000_0010b; 2 is power of two
0000_0000_0000_0100b; 4 is power of two
0000_0000_0000_1000b; 8 is power of two
0000_0000_0001_0000b; 16 is power of two
0000_0000_0010_0000b; 32 is power of two
0000_0000_0100_0000b; 64 is power of two
0000_0000_1000_0000b;128 is power of two
0000_0001_0000_0000b;256 is power of two
0000_0010_0000_0000b;512 is power of two
查看代码
package org.example;
import java.util.Random;
public class Singleton {
/**
* 1.instance = null;
* 2.int x = 0;
* 3.int y = 0;
* <p>
* instance = new Singleton();
* x++ => x=1
* y++ => y=1
* <p>
* x = 0;
* y = 1;
*/
private static final Singleton instance = new Singleton();// 饿汉式 在x,y上面,打印x=0,y=1
public static int x = 0;
public static int y;
/**
* int x = 0;
* int y = 0;
* instance = null;
*/
// private static final Singleton instance = new Singleton();// 饿汉式 在x,y 下面,打印x,y都返回1;在x,y上面,打印x=1,y=0
private Singleton() {
x++;
y++;
}
static Singleton getInstance() {
return instance;
}
public static void main(String[] args) {
// System.out.println("1.子类应用父类static常量,不会初始化子类类" + Child.age);
// 2. 定制应用数组,不会初始化类
Obj[] objs = new Obj[10];
System.out.println("2.定制应用数组,不会初始化类" + objs.length);
System.out.println("3.常量 salary = 100 不会初始化类" + Obj.salary);
System.out.println("4.复杂类型age = Random()会初始化类" + Obj.age);
// Singleton singleton = getInstance();
System.out.println("x=" + x);
System.out.println("y=" + y);
}
}
class Obj {
/**
* final 修饰的常量会在编译期间放到常量池中,不会初始化类
*/
public static final int salary = 100;
/**
* final 修饰的复杂类型,在编译期间无法计算得出,会初始化类
*/
public static final int age = new Random().nextInt(100);
static {
System.out.println(Obj.class.getSimpleName() + " class static block init!");
}
}
class Child extends Obj {
static {
System.out.println(Child.class.getSimpleName() + " class static block init!");
}
}
摘抄自网络,便于检索查找。