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

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

示例

下面我们再来看一个考虑负数的示例 −

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

更新于: 2019 年 9 月 24 日

99 浏览

开启您的职业生涯

完成课程即可获得认证

开始
广告