bitwise operators for finding less than in c

后端 未结 4 1440
离开以前
离开以前 2020-12-19 03:45

This is a homework assignment which requires me to come up with a function to determine if x < y, if it is I must return 1, using only bitwise o

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 04:40

    The idea of implementing subtraction is good.

    int sub = x + (~y+1);
    

    From here, you just need to check whether sub is negative, i.e. extract its sign bit. This is where the technical difficulty appears.

    Let's assume x and y are unsigned 32-bit integers. Then the result of subtraction can be represented by a signed 33-bit integer (check its minimum and maximum values to see that). That is, using the expression x + (~y+1) directly doesn't help, because it will overflow.

    What if x and y were 31-bit unsigned numbers? Then, x + (~y+1) can be represented by a 32-bit signed number, and its sign bit tells us whether x is smaller than y:

    answer(x, y) := ((x + (~y+1)) >> 31) & 1
    

    To convert x and y from 32 bits to 31 bits, separate their MSB (most significant bit) and all the other 31 bits into different variables:

    msb_x = (x >> 31) & 1;
    msb_y = (y >> 31) & 1;
    x_without_msb = x << 1 >> 1;
    y_without_msb = y << 1 >> 1;
    

    Then consider the 4 possible combinations of their MSB:

    if (msb_x == 0 && msb_y == 0)
        return answer(x_without_msb, y_without_msb);
    else if (msb_x == 1 && msb_y == 0)
        return 0; // x is greater than y
    else if (msb_x == 0 && msb_y == 1)
        return 1; // x is smaller than y
    else
        return answer(x_without_msb, y_without_msb);
    

    Converting all these if-statements to a single big ugly logical expression is "an exercise for the reader".

提交回复
热议问题