Java.lang.Long.toBinaryString() 方法
描述
java.lang.Long.toBinaryString() 方法将 long 参数的字符串表示形式返回为以 2 为底的无符号整数。
声明
以下是 java.lang.Long.toBinaryString() 方法的声明。
public static String toBinaryString(long i)
参数
i − 这是要转换为字符串的 long。
返回值
此方法以二进制(基数 2)返回参数表示的无符号长整型值的字符串表示形式。
异常
NA
示例
下面的例子展示了 java.lang.Long.toBinaryString() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class LongDemo {
public static void main(String[] args) {
long l = 210;
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));
// returns the number of one-bits
System.out.println("Number of one bits = " + Long.bitCount(l));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Number = 210 Binary is 11010010 Number of one bits = 4

