- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包附加内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包实用资源
- Java.lang - 实用资源
- Java.lang - 讨论
Java - Integer bitCount() 方法
描述
Java Integer bitCount() 方法返回指定 int 值 i 的二进制补码表示中 1 的位数。这有时被称为统计置位。
声明
以下是 java.lang.Integer.bitCount() 方法的声明
public static int bitCount(int i)
参数
i − 这是一个 int 值。
返回值
此方法返回指定 int 值的二进制补码表示中 1 的位数。
异常
无
正整数中 1 的位数示例
以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值一个正值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。
package com.tutorialspoint;
public class IntegerDemo {
public static void main(String[] args) {
int i = 177;
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));
}
}
输出
让我们编译并运行上面的程序,这将产生以下结果:
Number = 177 Binary = 10110001 Number of one bits = 4
负整数中 1 的位数示例
以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值一个负值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。
package com.tutorialspoint;
public class IntegerDemo {
public static void main(String[] args) {
int i = -177;
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));
}
}
输出
让我们编译并运行上面的程序,这将产生以下结果:
Number = -177 Binary = 11111111111111111111111101001111 Number of one bits = 29
正零整数中 1 的位数示例
以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值零值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。
package com.tutorialspoint;
public class IntegerDemo {
public static void main(String[] args) {
int i = 0;
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));
}
}
输出
让我们编译并运行上面的程序,这将产生以下结果:
Number = 0 Binary = 0 Number of one bits = 0
负零整数中 1 的位数示例
以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值负零值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。
package com.tutorialspoint;
public class IntegerDemo {
public static void main(String[] args) {
int i = -0;
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));
}
}
输出
让我们编译并运行上面的程序,这将产生以下结果:
Number = 0 Binary = 0 Number of one bits = 0
java_lang_integer.htm
广告