class Bits {
public static void main(String args[]) {
int n = 170; // 10101010
System.out.println("Value in binary: 10101010");
System.out.println("Number of one bits: " +
Integer.bitCount(n));
System.out.println("Highest one bit: " +
Integer.highestOneBit(n));
System.out.println("Lowest one bit: " +
Integer.lowestOneBit(n));
System.out.println("Number of leading zeros : " +
Integer.numberOfLeadingZeros(n));
System.out.println("Number of trailing zeros : " +
Integer.numberOfTrailingZeros(n));
System.out.println("\nBeginning with the value 1, " +
"rotate left 16 times.");
n = 1;
for(int i=0; i < 16; i++) {
n = Integer.rotateLeft(n, 1);
System.out.println(n);
}
}
}
Далее приведен вывод результатов работы программы из листинга I0.2:
Value in binary: 10101010
Number of one bits: 4
Highest one bit: 128
Lowest one bit: 2
Number of leading zeros: 24
Number of trailing zeros: 1
Begining with the value X, rotate left 16 times.
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
|