Codewars


	Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.
Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case
编写一个以整数作为输入的函数,并返回该数字的二进制表示中等于1的位数。您可以保证输入是非负的。

示例:1234is 的二进制表示10011010010,因此在这种情况下函数应该返回5

我的代码
public class BitCounting {
public static int countBits(int n){
// Show me the code!
int count = 0;
while(n != 0){
if((n & 1) == 1){
count++;
}
n = n>>>1;
}
return count;
}
}

最佳代码
public class BitCounting {
public static int countBits(int n){
return Integer.bitCount(n);
}
}

总结
对Java中已实现的功能及其不熟悉

CodeWar----求正整数二进制表示中1的个数的更多相关文章

  1. 转:对于一个字节(8bit)的变量,求其二进制表示中“1”的个数

    转:http://toutiao.com/a4280977370/ [解法一] 可以举一个八位的二进制例子来进行分析.对于二进制操作,我们知道,除以一个 2,原来的数字将会减少一个0.如果除的过程中有 ...

  2. int abs(int number)函数有感: 求补码和通过补码求对应的整数 C++(增加:数字的二进制表示中1的个数)

    #include "limits.h" #include "math.h" int abs(int number) { int const mask = num ...

  3. 《剑指offer》-统计整数二进制表示中1的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 直观思路就是把二进制表示从右往左统计1的个数.直接想到移位操作来迭代处理.坑点在于负数的移位操作会填充1.有人贴出了逻辑移位 ...

  4. 剑指offer11:输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。(进制转换,补码反码)

    1. 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 2. 思路和方法 使用移位(<<)和 “| & !”操作来实现.1的二进制是:前面都是0,最后一位 ...

  5. IT公司100题-28-整数的二进制表示中1的个数

    问题描述: 输入一个整数n,求n的二进制表示中,一共有多少个1.例如n=8,二进制表示为00001000,二进制表示中有1个1.     分析: 如果一个数n不为0,那么n-1的二进制表示,与n的二进 ...

  6. 正整数的二进制表示中1的个数计算(使用移位或者n&(n-1))

    第一种:使用n&(n-1)表示来计算有多少个1 int n=127; int count=0; while (n!=0){ count++; n=n&(n-1); } 第二种:使用移位 ...

  7. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  8. 22.整数二进制表示中1的个数[Get1BitCount]

    [题目] 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. [分析] 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数 ...

  9. C++ 一个整数的二进制表示中1的个数

    想知道某一位是否为1,只需和当前位对应的2的幂进行按位与运算即可. 如下示例,可以知道第6位是1,同理可知其他位是否为1,累加就能得到1的个数: 10001001 00000000 int cnt = ...

随机推荐

  1. ★房贷计算器 APP

    一.目的 1. 这是一个蛮有用的小工具 2. 之前看了很多demo,第一次来完全的自己实现一个APP 3. 完成之后提交 App Store 4. 作为Good Coder的提交审核材料 二.排期 周 ...

  2. js获取移动端触摸坐标

    想在touchmove事件里监听手指按下的坐标,event.pageX获取的是undefined,changedTouches,targetTouches,touches也只获得到了鼠标按下时的坐标, ...

  3. clock gate

    clock gate 这个专题,比较复杂设计DC  PT PR.下面仅仅从RTL行为级说明一下.

  4. python--subprocess,粘包现象与解决办法,缓冲区

    一. subprocess 的简单用法 import subprocess sub_obj = subprocess.Popen( 'dir', #系统指令 shell=True, #固定方法 std ...

  5. tensorboard以时间命名每一个文件夹

    tensorboard 有一个良好的命名习惯以时间命名每一个文件夹,例如**20190523_081232** ''' from datetiome import datetime dir = os. ...

  6. python index 自己实现

    l = [2,3,4,223,42,56,7,389,586,845,8,894,343,46,345,3556,23,233,45,25,78,456,785,576,344,6,34,563,] ...

  7. Python9-函数-day9

    初识函数定义与调用 def my_len(): i = 0 for k in s1: i +=1 return i #返回值 # s = 'tim' s1 = '班主任阿娇' length =my_l ...

  8. shell中test的使用

    #/secondin/secondfirstshecho “please enter two numseconder”read firstread secondif test $first -eq $ ...

  9. SVM 支持向量机算法介绍

    转自:https://zhuanlan.zhihu.com/p/21932911?refer=baina 参考:http://www.cnblogs.com/LeftNotEasy/archive/2 ...

  10. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)

    题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...