Mathematically Find Max Value without Conditional Comparison

前端 未结 14 585
暗喜
暗喜 2020-12-03 03:39

----------Updated ------------

codymanix and moonshadow have been a big help thus far. I was able to solve my problem using the equations and instead of using right

相关标签:
14条回答
  • 2020-12-03 04:00

    You can express this as a series of arithmetic and bitwise operations, e.g.:

    int myabs(const int& in) {
      const int tmp = in >> ((sizeof(int) * CHAR_BIT) - 1);
      return tmp - (in ^ tmp(;
    }
    
    int mymax(int a, int b) {
        return ((a+b) + myabs(b-a)) / 2;
    }
    
    0 讨论(0)
  • 2020-12-03 04:01
    #region GetMaximumNumber
    /// <summary>
    /// Provides method to get maximum values.
    /// </summary>
    /// <param name="values">Integer array for getting maximum values.</param>
    /// <returns>Maximum number from an array.</returns>
    private int GetMaximumNumber(params int[] values)
    {
      // Declare to store the maximum number.
      int maximumNumber = 0;
      try
      {
        // Check that array is not null and array has an elements.
        if (values != null &&
            values.Length > 0)
        {
          // Sort the array in ascending order for getting maximum value.
          Array.Sort(values);
    
          // Get the last value from an array which is always maximum.
          maximumNumber = values[values.Length - 1];
        }
      }
      catch (Exception ex)
      {
        throw ex;
      }
      return maximumNumber;
    }
    #endregion
    
    0 讨论(0)
  • 2020-12-03 04:02

    Solution without conditionals. Cast to uint then back to int to get abs.

    int abs (a) { return (int)((unsigned int)a); }
    int max (a, b) { return (a + b + abs(a - b)) / 2; }
    
    int max3 (a, b, c) { return (max(max(a,b),c); }
    
    0 讨论(0)
  • 2020-12-03 04:02
    //Assuming 32 bit integers 
    int is_diff_positive(int num)
    {
        ((num & 0x80000000) >> 31) ^ 1; // if diff positive ret 1 else 0
    }
    int sign(int x)
    {
       return ((num & 0x80000000) >> 31);
    }
    
    int flip(int x)
    {
       return x ^ 1;
    }
    
    int max(int a, int b)
    {
      int diff = a - b;
    
      int is_pos_a = sign(a);
      int is_pos_b = sign(b);
    
      int is_diff_positive = diff_positive(diff);
      int is_diff_neg = flip(is_diff_positive);
    
      // diff (a - b) will overflow / underflow if signs are opposite
      // ex: a = INT_MAX , b = -3 then a - b => INT_MAX - (-3) => INT_MAX + 3
      int can_overflow = is_pos_a ^ is_pos_b;
      int cannot_overflow = flip(can_overflow);
      int res = (cannot_overflow * ( (a * is_diff_positive) + (b * 
                is_diff_negative)) + (can_overflow * ( (a * is_pos_a) + (b * 
                is_pos_b)));
    
      return res;
    
    }
    
    0 讨论(0)
  • 2020-12-03 04:05

    please look at this program.. this might be the best answer till date on this page...

    #include <stdio.h>
    
    int main()
    {
        int a,b;
        a=3;
        b=5;
        printf("%d %d\n",a,b);
        b = (a+b)-(a=b); // this line is doing the reversal
        printf("%d %d\n",a,b);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-03 04:12

    If you can't trust your environment to generate the appropriate branchless operations when they are available, see this page for how to proceed. Note the restriction on input range; use a larger integer type for the operation if you cannot guarantee your inputs will fit.

    0 讨论(0)
提交回复
热议问题