题解 | #查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
import java.util.Scanner; /** * @author hll[**********] * @since 2023-03-18 23:37 **/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { System.out.println(bitCount(in.nextInt())); } } public static int bitCount(int i) { int count = i & 1; while (i > 0) { count += ((i >>= 1) & 1) == 1 ? 1 : 0; } return count; } }