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!");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章