zoukankan      html  css  js  c++  java
  • 11 二进制中1的个数

    题目描述

    输入一个整数,输出该数32位二进制表示中1的个数。其中负数用补码表示。

    因为golang的int是无限精度的,c++的int是32位的,所以golang的负数相当于前面有无限个1,要对golang的负数做处理.

    func NumberOf1( n int ) int {
        // write code here
        cnt := 0
        if n < 0 {
            n = n & 0xffffffff
        }
        for n != 0 {
            n = n & (n - 1)
            cnt++
        }
        return cnt
    }
  • 相关阅读:
    python re模块
    python
    python
    Django学习手册
    Django学习手册
    前端
    前端
    Django学习手册
    前端
    Database学习
  • 原文地址:https://www.cnblogs.com/dingxiaoqiang/p/14630102.html
Copyright © 2011-2022 走看看