当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.lang.Integer.numberOfLeadingZeros()用法及代码示例



描述

这个java.lang.Integer.numberOfLeadingZeros() 方法返回指定 int 值的二进制补码表示中 highest-order ("leftmost") one-bit 之前的零位数。

如果指定值在其二进制补码表示中没有 one-bits,换句话说,如果它等于 0,则返回 32。

声明

以下是声明java.lang.Integer.numberOfLeadingZeros()方法

public static int numberOfLeadingZeros(int i)

参数

i─ 这是 int 值。

返回值

此方法返回指定 int 值的二进制补码表示中 highest-order ("leftmost") one-bit 之前的零位数,如果该值等于零,则返回 32。

异常

NA

示例

下面的例子展示了 java.lang.Integer.numberOfLeadingZeros() 方法的用法。

package com.tutorialspoint;

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 = " + Integer.toBinaryString(i));

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

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

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

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

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

Number = 170
Binary = 10101010
Number of one bits = 4
Highest one bit = 128
Lowest one bit = 2
Number of leading zeros = 24

相关用法


注:本文由纯净天空筛选整理自 Java.lang.Integer.numberOfLeadingZeros() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。