main.java

来自「java的经典例子」· Java 代码 · 共 25 行

JAVA
25
字号
import java.math.*;

class Main {
    // Print the minimal representation of val
    static void printMinRep(long val) {
        BigInteger n  = BigInteger.valueOf(val);
        int blen = n.bitLength();
        System.out.print(n.signum() < 0 ? "1" : "0");
        for(int i=blen-1; i>=0; i--) {
            System.out.print(n.testBit(i) ? "1" : "0");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        printMinRep(0);     // 0
        printMinRep(1);     // 01
        printMinRep(0xff);  // 011111111
        printMinRep(-1);    // 1
        printMinRep(-0xff); // 100000001
        printMinRep(-3);    // 101
        printMinRep(5);     // 0101
    }
}

⌨️ 快捷键说明

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