Algorithm-Convert decimal number to binary number

public static void print(int a) {
        System.out.print(a + "的二进制数是:");
        for (int i = 31; i >= 0; i--) {
            System.out.print((a & (1 << i)) == (1 << i) ? "1" : "0");
        }
    }

Look at the output results:

The int type is 4 bytes, and 1 byte is 8 bits, which is 32 bits.

Perform & operation from left to right to the nth power of 2, and get me 2n power, which means that the current position is 1 or 0

2 to the nth power wins 1<<n to indicate

 

 

Guess you like

Origin blog.csdn.net/yanfei464486/article/details/111246735