zoukankan      html  css  js  c++  java
  • 查找数N二进制中1的个数(JS版 和 Java版)

    (function(){
    function getOne(n) {
      var c = 0;
      for(var i = 0;i < 32;i ++){
        if(((1 << i) & n) != 0){
            c++;
        }
      }
      return c;
    }
    console.log(getOne(10));
    console.log(getOne(-10));
    })();
    
    
    //比较最低位是否为1,复杂度O(logn),仅适合正数,当是负数,将出现死循环
    function getOne(n){
        var c = 0;
        while(n){
            if(n&1){
                c ++;
            }
            n >>= 1;
        }
        return c;
    }
    
    //同上,适合所有的数
    function getOne(n){
        var c = 0;
        while(n != 0){
            if((n & 1) != 0){
                c++;
            }
            n >>>= 1;
        }
        return c;
    }
    
    function getOne(n) {
      var c = 0;
      for (var i = 1; i; i <<= 1) {
        if (i & n) {
          c++;
        }
      }
      return c;
    }
    
    //int类型的是32位的数字,故只需要统计这32位上的数字的1的个数
    function getOne(n) {
      var c = 0;
      for(var i = 0;i < 32;i ++){
        if(((1 << i) & n) != 0){
            c++;
        }
      }
      return c;
    }
    
    
    //快速去掉最低位1
    function getOne(n){
        var c = 0;
        while(n){
            n = n &(n - 1);
            c++;
        }
        return c;
    }
    public class BinaryOneCount {
    
        public static void main(String[] args) {
            new BinaryOneCount().start();
        }
    
        private void start() {
            int n = -10;
            countOne(n);
    
            countOne(n = 10);
            countOne(n = 103);
            countOne(n = 30);
            
        }
    
        private void countOne(int n) {
            System.out.println(Integer.toBinaryString(n));
            System.out.println(Integer.bitCount(n));
            countBinaryOneBit(n);
            countBinaryOneBit1(n);
            countBinaryOneBit2(n);
        }
    
        /**
         * 查看当前最低位是否为1,是加1,否则就将当前数字无符号左移1位,进行下一轮判断
         * @param n
         */
        private void countBinaryOneBit(int n) {
            int c = 0;
            while (n != 0) {
                if ((n & 1) != 0) {
                    c++;
                }
                n >>>= 1;
            }
            System.out.println(c);
        }
    
        /**
         * 每次循环将最低位1去掉,这样进行的次数就是1的个数
         * @param n
         */
        private void countBinaryOneBit1(int n) {
            int c = 0;
            while (n != 0) {
                n &= (n - 1);
                c++;
            }
            System.out.println(c);
        }
        
        /**
         * 由于是32位的Int类型的数字,故只需要统计这32位上的数字1的个数
         * @param n
         */
        private void countBinaryOneBit2(int n){
            int c = 0;
            for(int i = 0;i < 32;i ++){
                if((n&(1<<i)) != 0){
                    c ++;
                }
            }
            System.out.println(c);
        }
    }
  • 相关阅读:
    最短路
    shell中奇怪的s文件状态判断
    诡异的PHPUnit安装
    安装ulipad的一点建议
    有关商业计算机的一点历史——引子
    Axure 不同菜单 页面不同位置
    Axure 选择不同内容, 跳转页面中加载不同内容
    Axure 侧边工具悬浮且位置固定
    Axure 多选和取消多选
    晒晒最近所做项目的总结
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/4310222.html
Copyright © 2011-2022 走看看