首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
懂二进制
[编程题]懂二进制
热度指数:4608
时间限制: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)
邀请回答
收藏(45)
分享
纠错
提交结果有问题?
28个回答
27篇题解
开通博客
君无颜
发表于 2022-03-19 16:57:05
两步走: 先异或,得到所有不同位 (r = m^n) 数r有多少位是1即可 c++实现 方法一 class Solution { public: int countBitDiff(int m, int n) { int r = m^n, res=0; wh
展开全文
空中转体一周半
发表于 2022-05-23 22:44:01
思路:二进制不同位,直接采用异或,然后采用循环右移计算1的个数即可。 Java: public class Solution { public int countBitDiff (int m, int n) { int c = m^n; int count =
展开全文
牛客82035003号
发表于 2022-03-29 20:49:26
//一个二进制数和1相与,就可以知道这个二进制数的最后一位是1还是0 //因为1和1相与还是1, 而0和1相与还是0 int countBitDiff(int m, int n ) { int
展开全文
姐姐的遮阳伞
发表于 2022-03-24 16:34:13
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n
展开全文
想五点下班勇士希望奇迹发生
发表于 2025-08-09 14:21:20
一行直接解决 class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型
展开全文
苏觅云
发表于 2022-05-19 13:28:04
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param
展开全文
苦行潜修者
发表于 2024-03-29 22:15:37
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m i
展开全文
lee≤e
发表于 2022-04-18 14:47:52
#想着把两个数字转为二进制字符串然后拿出长度最短的那个与最长的每一位做比较,最后加上长度差,但是只能a到6/10 数据 16807,282475249出错,GG # l1 = bin(m) # l2 = bin(n) # l3 = min(l1, l2
展开全文
fagtttttt
发表于 2023-08-06 21:30:35
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @retur
展开全文
牛客767752055号
发表于 2023-05-22 16:44:58
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param m int整型 * @param n int整型 * @retu
展开全文
问题信息
位运算
上传者:
牛客301499号
难度:
28条回答
45收藏
3754浏览
热门推荐
通过挑战的用户
查看代码
碧梨
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
相关试题
Skew数
基础数学
位运算
评论
(49)
旅行Ⅱ
动态规划
位运算
评论
(1)
Primary Arithmetic
字符串
基础数学
位运算
评论
(39)
下列选项中,管理员可以实现将普通开...
SQL
评论
(1)
当运算放大器工作在单位增益稳定状态...
模拟电路
评论
(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