C# 仅使用位运算符添加两个整数?

C# 仅使用位运算符添加两个整数?,c#,bit-manipulation,C#,Bit Manipulation,在C#中,是否可以不使用if..else、循环等来执行两个32位整数的求和 也就是说,是否可以只使用按位操作或()和(&)、异或(^)、而不是(!)、左移()?思考加法是如何一点一点地进行的。移位值以依次获得每个操作数的每一位,然后查看这两位的四个可能值,并计算出结果位应该是什么,以及是否需要担心进位。然后看看如何使用按位运算计算结果和进位。这里有一个示例供您参考 unsigned int myAdd(unsigned int a, unsigned int b) { unsigned

在C#中,是否可以不使用if..else、循环等来执行两个32位整数的求和


也就是说,是否可以只使用按位操作或(
)和(
&
)、异或(
^
)、而不是(
)、左移(
)?

思考加法是如何一点一点地进行的。移位值以依次获得每个操作数的每一位,然后查看这两位的四个可能值,并计算出结果位应该是什么,以及是否需要担心进位。然后看看如何使用按位运算计算结果和进位。

这里有一个示例供您参考

unsigned int myAdd(unsigned int a, unsigned int b)
{
    unsigned int carry = a & b;
    unsigned int result = a ^ b;
    while(carry != 0)
    {
        unsigned int shiftedcarry = carry << 1;
        carry = result & shiftedcarry;
        result ^= shiftedcarry;
    }
    return result;
}
unsigned int myAdd(unsigned int a,unsigned int b)
{
无符号整数进位=a&b;
无符号整数结果=a^b;
while(进位!=0)
{
无符号int-shiftedcarry=carry
公共静态int-getSum(int p,int q)
{
整数进位=0,结果=0;
对于(int i=0;i
静态int二进制添加(int x,int y)
{
而(x!=0)
{
int c=y&x;
y=y^x;
x=c试试这个:

    private int add(int a, int b) {
        if(b == 0)
            return a;

        return add( a ^ b, (a & b) << 1);
    }
private int add(int a,int b){
如果(b==0)
返回a;
返回add(a^b,(a&b)
intadd(inta,intb)
{
int结果=0,
//现在进位包含“a”和“b”的公共设置位
进位=a&b;
if(转换为oolean(进位))
{
//“a”和“b”的位之和,其中至少有一个
//未设置位的数目
结果=a^b;
//进位移动1,以便添加它
//“a”表示所需金额

出于好奇,你为什么要做这样的事?据他自己说,他只是想了解二进制数相加背后的逻辑。没什么特别的,只是为了知识。我还没上大学:(如果你对这类事情感兴趣,请阅读一本名为《确保你读过Tanenbaum的结构化计算机组织》的书。这是一本非常好的书,它从很多像这样的低级东西开始,然后就开始了。谢谢。这就是我要找的。现在我要试着去理解为什么这是可行的。tyCan任何人都能给出一个简洁的证明,为什么没有循环或递归的版本对于任意的表示大小都是不可能的?我要指出的是,这在现代Fortran中也应该适用,因为没有无符号整数类型。人们可以简单地使用普通整数,这个过程可以保证无符号整数加法模2^w,其中w是整数模型中的位数。在Fortran中,整数通常是2的补码,因此这些位将表示无符号整数,但如果打印整数的值,则会将这些位解释为有符号的2的补码整数。如果要使用大整数并验证加法是否有效,请仔细解释“NETETAKI:一方面考虑n个一个的情况,另一方面考虑一个一个比特。每次循环一次,进位就移动一个比特到左边。你必须循环至少N次,使进位向左移动到下一个比特或者进入结果进位。)- 1代码只提供。“int c=b | e”是或,不是加法。代码的其余部分只是将int转换为二进制字符串的一个坏例子。
i++
这不是加法吗?;)这里是否需要if语句,当b=0时,a^b=a,因此结果保持不变。@Anirudh,是的。您需要这个基本情况来结束递归。
public static int getSum(int p, int q)
{
    int carry=0, result =0;
    for(int i=0; i<32; i++)
    {
        int n1 = (p & (1<<(i)))>>(i); //find the nth bit of p
        int n2 = (q & (1<<(i)))>>(i); //find the nth bit of q

        int s = n1 ^ n2 ^ carry; //sum of bits
        carry = (carry==0) ? (n1&n2): (n1 | n2); //calculate the carry for next step
        result = result | (s<<(i)); //calculate resultant bit
    }

    return result;
}
static int binaryadd(int x, int y)
{
  while (x != 0)
  {
    int c = y & x;
    y = y ^ x; 
    x = c << 1;             
  }
  return y;
}
    private int add(int a, int b) {
        if(b == 0)
            return a;

        return add( a ^ b, (a & b) << 1);
    }
int Add(int a, int b)
{
      int result = 0,
          // carry now contains common set bits of "a" and "b"
          carry = a & b;

      if (Convert.ToBoolean(carry))
      {
          // Sum of bits of "a" and "b" where at least one 
          // of the bits is not set
          result = a ^ b;

          // carry is shifted by one so that adding it 
          // to "a" gives the required sum
          carry = carry << 1;

          result = add(carry, result);
      }
      else
      {
          result = a ^ b;
      }

      return result;
}