Java lang.Integer.toBinaryString() 方法
java.lang.Integer.toBinaryString() 方法将整数参数表示为 2 进制数字的字符串形式。
示例
以下是 Java 中实现 toBinaryString() 方法的示例 -
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("Num
}
}输出
Number = 170 Binary is 10101010 Number of one bits = 4
让我们看另一个考虑负数的示例 -
示例
import java.lang.*;
public class IntegerDemo {
public static void main(String[] args) {
int i = -35;
System.out.println("Number = " + i);
System.out.println("Binary is " + Integer.toBinaryString(i));
System.out.println("Number of one bits = " + Integer.bitCount(i));
}
}输出
Number = -35 Binary is 11111111111111111111111111011101 Number of one bits = 30
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP