當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.lang.Integer.numberOfTrailingZeros()用法及代碼示例



描述

這個java.lang.Integer.numberOfTrailingZeros() 返回指定 int 值的二進製補碼表示中最低階 ("rightmost") one-bit 之後的零位數。

如果指定值在其二進製補碼表示中沒有 one-bits,換句話說,如果它等於 0,則返回 32。

聲明

以下是聲明java.lang.Integer.numberOfTrailingZeros()方法

public static int numberOfTrailingZeros(int i)

參數

i─ 這是 int 值。

返回值

此方法返回指定 int 值的二進製補碼表示中 highest-order ("leftmost") one-bit 之前的零位數,如果該值等於零,則返回 32。

異常

NA

示例

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

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));

      /* returns the number of zero bits following the lowest-order 
         ("rightmost") one-bit */
      System.out.print("Number of trailing zeros = ");
      System.out.println(Integer.numberOfTrailingZeros(i));  
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

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

相關用法


注:本文由純淨天空篩選整理自 Java.lang.Integer.numberOfTrailingZeros() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。