Codewars Java练习:Bit Counting

题目

Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number.

Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case

题解

我使用了每次右移,统计奇偶性的解法——

标准解法中,我比较喜欢的有如下代码——

直接使用Integer的库函数

转换为字符串处理,其中用到了Java中的Lambda表达式

 

 

原创文章地址:【Codewars Java练习:Bit Counting】,转载时请注明出处mhy12345.xyz

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.