Mathematically Find Max Value without Conditional Comparison

前端 未结 14 634
暗喜
暗喜 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 03:54

    try this, (but be aware for overflows) (Code in C#)

        public static Int32 Maximum(params Int32[] values)
        {
            Int32 retVal = Int32.MinValue;
            foreach (Int32 i in values)
                retVal += (((i - retVal) >> 31) & (i - retVal));
            return retVal;        
        }
    

提交回复
热议问题