首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
懂二进制
[编程题]懂二进制
热度指数:3867
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
示例1
输入
3,5
输出
2
说明
3的二进制为11,5的二进制为101,总共有2位不同
示例2
输入
1999,2299
输出
7
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
查看答案及解析
添加笔记
求解答(0)
邀请回答
收藏(35)
分享
纠错
提交结果有问题?
2个回答
21篇题解
添加回答
0
C
猪猪侠新新酱
//负数在内存中存储的就是补码
//先是计算两个数的异或结果 相同为0 不同为1。最后根据这结果判断有几个1.
int
countBitDiff
(
int
m
,
int
n
) {
// write code here
int
i=
0
;
//int ans=0;
int
ans=m^n;
int
count=
0
;
while
(i<
32
){
count+=(ans&
1
);
i++;
ans=ans>>
1
;
}
return
count;
}
发表于 2022-12-23 17:08:25
回复(0)
0
C
牛客310270168号
int countBitDiff(int m, int n ) {
// write code here
int count=0;
int s=m^n;
int mask;
for(int i=0;i<32;i++)
{
mask=1<<i;
if(s&mask) count++;
}
return count;
}
发表于 2022-08-08 09:53:36
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
位运算
上传者:
牛客301499号
难度:
2条回答
35收藏
1564浏览
热门推荐
通过挑战的用户
查看代码
碧梨
2022-10-03 22:31:02
Flawles...
2022-09-16 16:20:28
牛客96860...
2022-09-16 10:51:14
牛客16979...
2022-09-15 22:39:21
Tommyle...
2022-09-15 22:24:30
相关试题
Primary Arithmetic
字符串
基础数学
位运算
评论
(39)
Skew数
基础数学
位运算
评论
(48)
旅行Ⅱ
动态规划
位运算
评论
(1)
之前的经历中单品数据分析的经验丰富...
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ public int countBitDiff (int m, int n) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ int countBitDiff(int m, int n) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param m int整型 # @param n int整型 # @return int整型 # class Solution: def countBitDiff(self , m , n ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ public int countBitDiff (int m, int n) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ function countBitDiff( m , n ) { // write code here } module.exports = { countBitDiff : countBitDiff };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param m int整型 # @param n int整型 # @return int整型 # class Solution: def countBitDiff(self , m: int, n: int) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ func countBitDiff( m int , n int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ int countBitDiff(int m, int n ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param m int整型 # @param n int整型 # @return int整型 # class Solution def countBitDiff(m, n) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ def countBitDiff(m: Int,n: Int): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ fun countBitDiff(m: Int,n: Int): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ public int countBitDiff (int m, int n) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ export function countBitDiff(m: number, n: number): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ func countBitDiff ( _ m: Int, _ n: Int) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @return int整型 */ pub fn countBitDiff(&self, m: i32, n: i32) -> i32 { // write code here } }
3,5
2
1999,2299
7