Java.lang.Integer.toBinaryString() 方法
描述
java.lang.Integer.toBinaryString() 方法将整数参数的字符串表示形式返回为基数为 2 的无符号整数。
声明
以下是 java.lang.Integer.toBinaryString() 方法的声明。
public static String toBinaryString(int i)
参数
i − 这是一个要转换为字符串的整数。
返回值
此方法以二进制(基数 2)返回参数表示的无符号整数值的字符串表示形式。
异常
NA
示例
下面的例子展示了 java.lang.Integer.toBinaryString() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class IntegerDemo {
public static void main(String[] args) {
int i = 170;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned integer value
represented by the argument in binary (base 2) */
System.out.println("Binary is " + Integer.toBinaryString(i));
// returns the number of one-bits
System.out.println("Number of one bits = " + Integer.bitCount(i));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Number = 170 Binary is 10101010 Number of one bits = 4

