Java.lang.Long.numberOfTrailingZeros() 方法



描述

java.lang.Long.numberOfTrailingZeros() 方法返回指定长整数值的二进制补码表示中最低位(“最右边”)的1位之后零位的数量。如果指定值在其二进制补码表示中没有1位,换句话说,如果它等于零,则返回64。

声明

以下是java.lang.Long.numberOfTrailingZeros() 方法的声明

public static int numberOfTrailingZeros(long i)

参数

i − 这是长整数值。

返回值

此方法返回指定长整数值的二进制补码表示中最低位(“最右边”)的1位之后零位的数量,如果该值为零,则返回64。

异常

从正值的长整型中获取最低位1位之后零位的数量示例

以下示例演示了如何使用 Long numberOfTrailingZeros() 方法获取最低位1位之后零位的数量。我们创建了一个长整型变量并为其赋值一个正的长整数值。然后使用 toBinaryString() 方法打印值的二进制格式。使用 bitCount() 打印1位的数量。使用 highestOneBit() 打印最高位。使用 lowestOneBit() 打印最低位,然后使用 numberOfTrailingZeros() 方法打印最低位1位之后零位的数量。

package com.tutorialspoint;

public class LongDemo {
   public static void main(String[] args) {
      long i = 170L;
      System.out.println("Number = " + i);
    
      /* returns the string representation of the unsigned long value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Long.toBinaryString(i));

      // returns the number of one-bits 
      System.out.println("Number of one bits = " + Long.bitCount(i));

      /* returns an long value with at most a single one-bit, in the position 
         of the highest-order ("leftmost") one-bit in the specified long value */
      System.out.println("Highest one bit = " + Long.highestOneBit(i));

      /* returns an long value with at most a single one-bit, in the position
         of the lowest-order ("rightmost") one-bit in the specified long value.*/
      System.out.println("Lowest one bit = " + Long.lowestOneBit(i));

      /*returns the number of zero bits preceding the highest-order 
         ("leftmost")one-bit */
      System.out.print("Number of trailing zeros = ");
      System.out.println(Long.numberOfTrailingZeros(i));
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = 170
Binary = 10101010
Number of one bits = 4
Highest one bit = 128
Lowest one bit = 2
Number of trailing zeros = 1

从负值的长整型中获取最低位1位之后零位的数量示例

以下示例演示了如何使用 Long numberOfTrailingZeros() 方法获取最低位1位之后零位的数量。我们创建了一个长整型变量并为其赋值一个负的长整数值。然后使用 toBinaryString() 方法打印值的二进制格式。使用 bitCount() 打印1位的数量。使用 highestOneBit() 打印最高位。使用 lowestOneBit() 打印最低位,然后使用 numberOfTrailingZeros() 方法打印最低位1位之后零位的数量。

package com.tutorialspoint;

public class LongDemo {
   public static void main(String[] args) {
      long i = -170L;
      System.out.println("Number = " + i);
    
      /* returns the string representation of the unsigned long value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Long.toBinaryString(i));

      // returns the number of one-bits 
      System.out.println("Number of one bits = " + Long.bitCount(i));

      /* returns an long value with at most a single one-bit, in the position 
         of the highest-order ("leftmost") one-bit in the specified long value */
      System.out.println("Highest one bit = " + Long.highestOneBit(i));

      /* returns an long value with at most a single one-bit, in the position
         of the lowest-order ("rightmost") one-bit in the specified long value.*/
      System.out.println("Lowest one bit = " + Long.lowestOneBit(i));

      /*returns the number of zero bits preceding the highest-order 
         ("leftmost")one-bit */
      System.out.print("Number of trailing zeros = ");
      System.out.println(Long.numberOfTrailingZeros(i));
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = -170
Binary = 1111111111111111111111111111111111111111111111111111111101010110
Number of one bits = 60
Highest one bit = -9223372036854775808
Lowest one bit = 2
Number of trailing zeros = 1

从值为零的长整型中获取最低位1位之后零位的数量示例

以下示例演示了如何使用 Long numberOfTrailingZeros() 方法获取最低位1位之后零位的数量。我们创建了一个长整型变量并为其赋值一个值为零的长整数值。然后使用 toBinaryString() 方法打印值的二进制格式。使用 bitCount() 打印1位的数量。使用 highestOneBit() 打印最高位。使用 lowestOneBit() 打印最低位,然后使用 numberOfTrailingZeros() 方法打印最低位1位之后零位的数量。

package com.tutorialspoint;

public class LongDemo {
   public static void main(String[] args) {
      long i = 0L;
      System.out.println("Number = " + i);
    
      /* returns the string representation of the unsigned long value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Long.toBinaryString(i));

      // returns the number of one-bits 
      System.out.println("Number of one bits = " + Long.bitCount(i));

      /* returns an long value with at most a single one-bit, in the position 
         of the highest-order ("leftmost") one-bit in the specified long value */
      System.out.println("Highest one bit = " + Long.highestOneBit(i));

      /* returns an long value with at most a single one-bit, in the position
         of the lowest-order ("rightmost") one-bit in the specified long value.*/
      System.out.println("Lowest one bit = " + Long.lowestOneBit(i));

      /*returns the number of zero bits preceding the highest-order 
         ("leftmost")one-bit */
      System.out.print("Number of trailing zeros = ");
      System.out.println(Long.numberOfTrailingZeros(i));
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = 0
Binary = 0
Number of one bits = 0
Highest one bit = 0
Lowest one bit = 0
Number of trailing zeros = 64
java_lang_long.htm
广告
© . All rights reserved.