带有示例的Java lang.Long.toBinaryString()方法
java.lang.Long.toBinaryString()方法返回long参数的字符串表示形式,为基数2中的无符号整数。
示例
以下是实现toBinaryString()方法的示例-
import java.lang.*;
public class Demo {
public static void main(String[] args) {
long l = 190;
System.out.println("Number = " + l);
/* returns the string representation of the unsigned long value
represented by the argument in binary (base 2) */
System.out.println("Binary is " + Long.toBinaryString(l));
//返回一位数
System.out.println("Number of one bits = "
}
}输出结果
Number = 190 Binary is 10111110 Number of one bits = 6
示例
让我们看看另一个考虑负数的例子-
import java.lang.*;
public class Demo {
public static void main(String[] args) {
long l = -25;
System.out.println("Number = " + l);
System.out.println("Binary is " + Long.toBinaryString(l));
System.out.println("Number of one bits = " + Long.bitCount(l));
}
}输出结果
Number = -25 Binary is 1111111111111111111111111111111111111111111111111111111111100111 Number of one bits = 62