Bitwise Less than or Equal to

为君一笑 提交于 2019-12-01 19:01:09

If x > y, then y - x or (y + (~x + 1)) will be negative, hence the high bit will be 1, otherwise it will be 0. But we want x <= y, which is the negation of this.

    /*
     * isLessOrEqual - if x <= y  then return 1, else return 0 
     *   Example: isLessOrEqual(4,5) = 1.
     *   Legal ops: ! ~ & ^ | + << >>
     *   Max ops: 24
     *   Rating: 3
     */
    int isLessOrEqual(int x, int y)
    {
        return !(((y + (~x + 1)) >> 31) & 1);
    }

Even better, drop the shift operator and use a bit mask on the high bit:

    int isLessOrEqual(int x, int y)
    {
        return !((y + (~x + 1)) & 0x80000000);
    }

EDIT:

As a commenter pointer out, the above version is susceptible to arithmetic overflow errors. Here is another version that covers the edge cases.

#include <limits>
int isLessOrEqual(int x, int y)
{
    static int const vm = std::numeric_limits<int>::max();
    static int const sm = ~vm;

    return  !! ((x & ~y | ~(x ^ y) & ~((y & vm) + ~(x & vm) + 1)) & sm);
}

Explanation: the overall strategy is to treat the sign bit of the inputs as logically distinct from the rest of the bits, the "value bits," and perform the subtraction as in the previous example on just the value bits. In this case, we need only perform the subtraction where the two inputs are either both negative or both non-negative. This avoids the arithmetic overflow condition.

Since the size of int strictly speaking is unknown at run time, we use std::numeric_limits<int>::max() as a convenient mask for the value bits. The mask of the sign bit is simply the bit-wise negation of the value bits.

Turning to the actual expression for <=, we factor out the bit-wise mask sm of the sign bit in each of the sub-expressions and push the operation to the outside of the expression. The first term of the logical expression x & ~y is true when x is negative and y is non-negative. The first factor of the next term ~(x ^ Y) is true when both are negative or both are non-negative. The second factor ~((y & vm) + ~(x & vm) + 1)) is true when y - x is non-negative, in other words x <= y, ignoring the sign bit. The two terms are or'd, so using c++ logical expression syntax we have:

x < 0 && y >= 0 || (x < 0 && y < 0 || x >= 0 && y >= 0) && y - x >= 0

The !! outermost operators convert the raised sign bit to a 1. Finally, here is the Modern C++ templated constexpr version:

template<typename T>
constexpr T isLessOrEqual(T x, T y)
{
    using namespace std;
    // compile time check that type T makes sense for this function
    static_assert(is_integral<T>::value && is_signed<T>::value, "isLessOrEqual requires signed integral params");

    T vm = numeric_limits<T>::max();
    T sm = ~vm;

    return  !! ((x & ~y | ~(x ^ y) & ~((y & vm) + ~(x & vm) + 1)) & sm);
}
Yanagar1

Those functions don't fully work because of the overflow, so that's how I solved the problem. Eh...

int isLessOrEqual(int x, int y) {
int diff_sgn = !(x>>31)^!(y>>31);      //is 1 when signs are different
int a = diff_sgn & (x>>31);            //diff signs and x is neg, gives 1
int b = !diff_sgn & !((y+(~x+1))>>31); //same signs and difference is pos or = 0, gives 1
int f = a | b;
return f;
}

Really enjoyed Yanagar1's answer, which is very easy to understand.

Actually we can remove those shift operators and use De Morgan's laws, which reduce the number of operators from 15 to 11.

long isLessOrEqual(long x, long y) {
  long sign = (x ^ y);               // highest bit will be 1 if different sign
  long diff = sign & x;              // highest bit will be 1 if diff sign and neg x
  long same = sign | (y + (~x + 1)); // De Morgan's Law with the following ~same
                                     // highest bit will be 0 if same sign and y >= x
  long result = !!((diff | ~same) & 0x8000000000000000L); // take highest bit(sign) here
  return result;
}

Here is my implementation(spend around 3 hours...)

int
isLessOrEqual(int x, int y)
{
    int a = y + ~x + 1;
    int b = a & 1 << 31 & a; // !b => y >= x, but maybe overflow
    int c = !!(x & (1 << 31)) & !(y & (1 << 31)); // y > 0, x < 0
    int d = !(x & (1 << 31)) & !!(y & (1 << 31)); // x > 0, y < 0

    int mask1 = !c + ~0;

    // if y > 0 && x < 0, return 1. else return !b
    int ans = ~mask1 & !b | mask1 & 1;

    int mask2 = !d + ~0;

  // if y < 0 && x > 0, return 0, else return ans
    return ~mask2 & ans | mask2 & 0;
}
  • y - x == y + ~x + 1

  • a & 1 << 31 & a is simplify from !(!(a & (1 << 31)) | !a)

The logic is:

if `y > 0 && x < 0`
    return true  
if `x > 0 && y < 0`
    return false

return y >= x

Why not just y >= x directly? because overflow may happen. So I have to early return to avoid overflow.

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