Java lang.Integer.toBinaryString()方法
java.lang.Integer.toBinaryString()方法返回整数参数的字符串表示形式,以基数2为无符号整数。
示例
以下是toBinaryString()在Java中实现该方法的示例-
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));
//返回一位数
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
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短