首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
另类加法
[编程题]另类加法
热度指数:8263
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 32M,其他语言64M
算法知识视频讲解
给定两个int
A
和
B
。编写一个函数返回A+B的值,但不得使用+或其他算数运算符。
测试样例:
1,2
返回:3
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(14)
邀请回答
收藏(120)
分享
纠错
提交结果有问题?
55个回答
10篇题解
开通博客
大桔骑士v
发表于 2020-07-20 22:37:28
思路 试想二进制0101和1101的相加过程0 1 0 11 1 0 1其实可以看成是不带进位的结果1000和进位产生的1010相加。 而“不带进位的加法”其实就是异或运算,“进位”其实就是只有两个1的时候才会出现,也就是与运算,只是因为进了一位,所以还要往左移动一位。 这样就将两个数相加通过位运算
展开全文
粉星星
发表于 2023-03-31 19:02:18
int addAB(int A, int B) { while (B != 0) { int carry = (unsigned int)(A & B) << 1; // 计算进位部分 A ^= B; // 计算不进位部分
展开全文
知道石头呢
发表于 2026-01-17 16:25:53
class UnusualAdd { public: int addAB(int A, int B) { // write code here if(B == 0) return A; return (addAB(A^B,(A&
展开全文
阿贝尔的日记
发表于 2022-09-23 17:09:21
另类加法 另类加法 /* 2022年09月21日 11:43:09 异或是不进位的加法 0001 ^ 0011 0010 相与后左移一位即可得到进位的数据 0001 & 0011 0001 << 1 0010 0010 ^ 0010 0000 0010 & 001
展开全文
硌手的小虫子
发表于 2023-03-31 15:55:41
import java.util.*; public class UnusualAdd { public static int addAB(int A, int B) { if(B==0){ return A; } w
展开全文
黑眼X
发表于 2025-09-27 15:12:37
import java.util.*; public class UnusualAdd { public int addAB(int A, int B) { int C = 0; while (B != 0) { C = A ^ B
展开全文
Dfine
发表于 2025-07-07 16:48:14
class UnusualAdd { public: int addAB(int A, int B) { // write code here while (B) { int sum = A ^ B; int c
展开全文
牛客710153440号
发表于 2025-09-04 22:35:04
class UnusualAdd { public: int addAB(int A, int B) { // write code here /* 以 A=3(011),B=5(101) 为例,递归过程如下: 第一次调用:C=3^5=6(110),D=(3
展开全文
果粒陈33
发表于 2022-06-20 15:36:18
# -*- coding:utf-8 -*- class UnusualAdd: def addAB(self, A, B): # write code here while B: C = A ^ B B =
展开全文
牛马ID
发表于 2022-05-29 13:51:14
class UnusualAdd { public: int addAB(int a, int b) { // if(a == 0) return b; // if(b == 0) return a; // int on = a ^ b; //
展开全文
问题信息
位运算
难度:
55条回答
120收藏
15474浏览
热门推荐
通过挑战的用户
查看代码
鹰不泊wyk
2023-03-06 00:58:16
牛客95938...
2023-02-13 18:43:11
36度的手指编程
2023-02-08 23:53:10
橘子拒绝内卷
2023-02-03 15:47:11
牛客17366...
2023-01-28 17:13:32
相关试题
Primary Arithmetic
字符串
基础数学
位运算
评论
(39)
旅行Ⅱ
动态规划
位运算
评论
(1)
Skew数
基础数学
位运算
评论
(49)
《绝地求生》中,每局游戏最多有多少...
游戏常识
评论
(1)
动态餐厅定价需要实时显示,延迟较低...
大模型开发
评论
(1)
另类加法
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class UnusualAdd { public int addAB(int A, int B) { // write code here } }
class UnusualAdd { public: int addAB(int A, int B) { // write code here } };
# -*- coding:utf-8 -*- class UnusualAdd: def addAB(self, A, B): # write code here
class UnusualAdd { public int addAB(int A, int B) { // write code here } }