博客
关于我
第28题:求整数的二进制表示中1的个数
阅读量:529 次
发布时间:2019-03-08

本文共 951 字,大约阅读时间需要 3 分钟。

?????????????????????????????????1??????????????????????

????

??????????????????????????

  • ???????????1????
  • ?????????????????0?
  • ???????????????1????????????
  • ????????????????1????
  • ????

    package test028;public class Test028 {    public static int getNumOfOne(int n) {        if (n < 0) {            return -1; // ??????????????        }        int count = 0;        while (n != 0) {            if ((n & 1) == 1) {                count++;            }            n >>= 1;        }        return count;    }    public static void main(String[] args) {        System.out.println("11???????? " + getNumOfOne(11) + " ?1?");    }}

    ????

  • ????????????????????-1????????????????????
  • ???????int count = 0; ????1????
  • ?????while (n != 0)????n??0???????
  • ??????if ((n & 1) == 1)???n????????1????????????
  • ?????n >>= 1; ?n?????????????
  • ?????System.out.println("11???????? " + getNumOfOne(11) + " ?1?"); ?????????????
  • ??????????????????????1??????????O(log n)???????????????

    转载地址:http://hbciz.baihongyu.com/

    你可能感兴趣的文章
    PHP5.4 + IIS + Win2008 R2 配置
    查看>>
    Redis从入门到精通
    查看>>
    PHP5.6.x编译报错:Don't know how to define struct flock on this system, set --enable-opcache=no
    查看>>
    php5ts.dll 下载_php5ts.dll下载
    查看>>
    php7
    查看>>
    PHP7 新特性
    查看>>
    PHP7+MySQL5.7+Nginx1.9. on Ubuntu 14.0
    查看>>
    php7.1.6 + redis
    查看>>
    php7中使用php_memcache扩展
    查看>>
    PHP7中十个需要避免的坑
    查看>>
    php7和PHP5对比的新特性和性能优化
    查看>>
    PHP7安装pdo_mysql扩展
    查看>>
    PHP7实战开发简单CMS内容管理系统(7) 后台登录架构 用户登录校验
    查看>>
    php7,从phpExcel升级到PhpSpreadsheet
    查看>>
    PHP8.1 + ThinkPHP实战指南:高效构建现代化网站的六大技巧
    查看>>
    PHP8中match新语句的操作方法
    查看>>
    PHP:第一章——PHP中常量和预定义常量
    查看>>
    PHP:第一章——PHP中的位运算
    查看>>
    phpcms
    查看>>
    phpcms 2008 product.php pagesize参数代码注射漏洞
    查看>>