Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

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

I misunderstood a question that said to add two integers using bitwise operations. I did not use any control flow and could not do it. After giving up, all the solutions I found use control flow to accomplish this whether it be an if, while, for, recursion, etc,. Is there a proof that is cancannot be accomplished?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...