带有示例的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));
//返回一位数
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
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短