Is it possible to write a function adding two integers without control flow and strictly bitwise operations?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 16:03:36

For a fixed length integer, you can just unroll a ripple carry adder. In the worst case, a carry signal has to propagate all the way from the least significant bit to the most significant bit.

Like this (only slightly tested) (to avoid the C-purists' wrath, I will call this C# code)

int add_3bits(int x, int y)
{
    int c = x & y;
    x = x ^ y;
    y = c << 1;
    //
    c = x & y;  //  \
    x = x ^ y;  //  | for more bits, insert more of these blocks
    y = c << 1; //  /
    //
    // optimized last iteration
    return (x ^ y) & 7; // for more bits, change that mask
}

If you do it for as many bits as your integer will hold, you won't need the mask in the end.

That's not very efficient, clearly. For 3 bits it's fine, but for 32 bits it becomes quite long. A Kogge-Stone adder (one of the O(log n) delay adder circuits) is also surprisingly easy to implement in software (in hardware you have to deal with a lot of wires, software doesn't have that problem).

For example: (verified using my website)

static uint add_32bits(uint x, uint y)
{
    uint p = x ^ y;
    uint g = x & y;

    g |= p & (g << 1);
    p &= p << 1;

    g |= p & (g << 2);
    p &= p << 2;

    g |= p & (g << 4);
    p &= p << 4;

    g |= p & (g << 8);
    p &= p << 8;

    g |= p & (g << 16);

    return x ^ y ^ (g << 1);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!