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);
        }
    }
  • 相关阅读:
    jQuery插件之jquery editable plugin点击编辑文字插件
    firefox与ie的javascript兼容性编程汇编【转载】
    css前端制作 经验总结
    非常棒的jqChart图表插件
    WPF Image Source设置文件路径后 在编辑状态下显示图片,运行时不显示
    WPF RadioButton 绑定枚举
    WPF MVVM实现数据增删改查逻辑全流程详细解析demo
    bigNumber.js的简单使用
    PHP程序的“Missing argument 3”的错误提示解决方法
    PHP判断0和空的方法
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/4310222.html
Copyright © 2011-2022 走看看