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));
// returns the number of one-bits
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP