MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / printBinary

Method printBinary

src/class003/BinarySystem.java:13–26  ·  view source on GitHub ↗
(int num)

Source from the content-addressed store, hash-verified

11 // 打印一个int类型的数字,32位进制的状态
12 // 左侧是高位,右侧是低位
13 public static void printBinary(int num) {
14 for (int i = 31; i >= 0; i--) {
15 // 下面这句写法,可以改成 :
16 // System.out.print((a & (1 << i)) != 0 ? "1" : "0");
17 // 但不可以改成 :
18 // System.out.print((a & (1 << i)) == 1 ? "1" : "0");
19 // 因为a如果第i位有1,那么(a & (1 << i))是2的i次方,而不一定是1
20 // 比如,a = 0010011
21 // a的第0位是1,第1位是1,第4位是1
22 // (a & (1<<4)) == 16(不是1),说明a的第4位是1状态
23 System.out.print((num & (1 << i)) == 0 ? "0" : "1");
24 }
25 System.out.println();
26 }
27
28 public static void main(String[] args) {
29 // 非负数

Callers 1

mainMethod · 0.95

Calls 2

printMethod · 0.45
printlnMethod · 0.45

Tested by

no test coverage detected