如何使用位移和放大器将unsigned int递增1.只是逻辑上的opr? [英] How to Increment unsigned int by 1 using bit-shifting & logical opr only?
本文介绍了如何使用位移和放大器将unsigned int递增1.只是逻辑上的opr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的作业/项目中有一个问题,仅使用 bit-shifting 和逻辑运算符将1加到无符号整数中.函数中不应包含任何"+",-","*"或"/"符号.
I have a question in my assignment / project that adds 1 to an unsigned integer only using bit-shifting and logical operators. There shouldn't be any "+", "-", "*", or "/" symbols in the function.
我从最后几天开始尝试,但尚未成功.到目前为止,我已经尝试了以下方法:
I am trying from last days but no success yet. So far I've tried the following:
int A = (((B&C)<<1)^(B^C))
有人可以帮我解决这个问题吗?
Can anybody help me to solve this.?
您可以使用任何编程语言来帮助我.
You can help me in any programming language.
推荐答案
unsigned int i = ...;
unsigned int mask = 1;
while (i & mask)
{
i &= ~mask;
mask <<= 1;
}
i |= mask;
这篇关于如何使用位移和放大器将unsigned int递增1.只是逻辑上的opr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文