题解 | 二进制不同位数

二进制不同位数

https://www.nowcoder.com/practice/daf9032926614dab91ca624a7759a868

计算m和n二进制表示上的不同位数,等价于计算m异或n的值中二进制'1'的个数

C++版本

#include <iostream>

int main(){
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);

  int m,n;
  std::cin >> m >> n;

  std::cout << __builtin_popcount(m ^ n) << "\n";
}

Python版本

import sys

input = lambda : sys.stdin.readline().strip()

m,n = map(int,input().split())

print(bin(m ^ n).count('1'))

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务