Java Integer.bitCount()方法

原文:https://www.studytonight.com/java-wrapper-class/java-integer-bitcount-method

Java bitCount()方法属于Integer类。此方法用于将一个一位数返回为指定整数值的二进制补码形式,并对二进制序列中的设置位数进行计数。

这个方法将一个整数作为参数,并返回一个整数。例如,如果给定的输入是 1000111110 ,那么这个方法应该返回 6 ,因为在这个输入中有 6 设置位(1)

语法:

public static int bitCount(int i)

参数:

传递的唯一参数是整数,它的二进制补码形式将被返回。

返回:

该方法将一位数返回为整数的二进制补码形式,因此其返回类型也是int.

例 1:

这里,我们输入一个整数 365Integer.toBinaryString()方法会将数字转换为其等效的二进制字符串( 101101101 为整数 365 )。然后转换后的二进制数通过Integer.countBit()对位数进行计数。

import java.lang.Integer;

public class StudyTonight {
  public static void main(String[] args) {
    int i = 365;
    int j = -365;
    System.out.println("Actual Number is " + i);

    // converting the number to its equivalent binary form with base 2 
    System.out.println("Binary conversion of" + i + " = " + Integer.toBinaryString(i));

    //returning the number of one-bits 
    System.out.println("Number of one bits = " + Integer.bitCount(i));
    System.out.println("*******************");
    System.out.println("Actual Number is " + j);
    System.out.println("Binary conversion of" + j + " = " + Integer.toBinaryString(j));
    System.out.println("Number of one bits = " + Integer.bitCount(j));
  }
}

实际数为 365 二进制转换的 365 = 101101101 一位数= 6

                              • T4】实际数为-365 二进制转换的-365 = 111111111111111010010011 一位数= 27

例 2:

这里,我们取一个整数数组。Integer.toBinaryString()方法将把数组中的数字转换成其等价的二进制字符串。二进制字符串在通过countBit()方法之前需要转换成整数,这是通过Integer.parseInt()方法完成的,然后转换后的二进制数通过Integer.countBit()来计数位数。

import java.lang.Integer;

public class StudyTonight {
  public static void main(String args[]) {
    int[] set = {
      72,
      78,
      56,
      89,
      80,
      66
    };
    try {
      for (int i = 0; i < 6; i++) {
        String b = Integer.toBinaryString(set[i]); //Converting the actual number to binary String
        int c = Integer.parseInt(b, 2); //Converting the binary String to binary integer of base 2
        int d = Integer.bitCount(c); //Counting the set bits
        System.out.println("Actual number is " + set[i] + " binary sequence : " + b + ",number of set bits are : " + d);
      }
    }
    catch(Exception e) {
      System.out.println("Exception occurred..");
    }
  }
}

实际数为 72 二进制序列:1001000,设置位数为:2 实际数为 78 二进制序列:1001110,设置位数为:4 实际数为 56 二进制序列:111000,设置位数为:3 实际数为 89 二进制序列:1011001,设置位数为:4 实际数为 80 二进制序列:1010000,设置位数

例 3:

这里有一个通用的例子,用户可以输入他选择的数字并获得所需的输出。trycatch块用于处理程序执行期间发生的任何异常。(例如,用户输入字符串等。代替整数)。

import java.lang.Integer;
import java.util. * ;

public class StudyTonight {

  public static void main(String[] args) {

    System.out.println("Enter number");
    Scanner sc = new Scanner(System. in );
    try {
      int i = sc.nextInt();
      System.out.println("Actual Number is " + i);

      // converting the number to its equivalent binary form with base 2 
      System.out.println("Binary conversion of" + i + " = " + Integer.toBinaryString(i));

      //returning the number of one-bits 
      System.out.println("Number of one bits = " + Integer.bitCount(i));
    }
    catch(Exception e) {
      System.out.println("Error!!");
    }
  }
}

输入数字 67 实际数字为 67 二进制转换 67 = 1000011 一位数= 3

                                                        • *输入数字 0x896 错误!!

实时示例:

在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。