Java lang.Long.toBinaryString() 方法示例
java 8object oriented programmingprogramming更新于 2025/12/5 18:52:17
java.lang.Long.toBinaryString() 方法返回 long 参数的字符串表示形式,该字符串表示形式为二进制无符号整数。
示例
以下是实现 toBinaryString() 方法 − 的示例
import java.lang.*;
public class Demo {
public static void main(String[] args) {
long l = 190;
System.out.println("Number = " + l);
/* 返回参数以二进制(基数 2)表示的无符号长整型值的字符串表示形式
*/
System.out.println(&"Binary is &" + Long.toBinaryString(l));
// 返回 1 的位数
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

