Mathematically Find Max Value without Conditional Comparison

前端 未结 14 635
暗喜
暗喜 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

    finding the maximum of 2 variables:

    max = a-((a-b)&((a-b)>>31))

    where >> is bitwise right-shift (also called SHR or ASR depeding on signedness).

    Instead of 31 you use the number of bits your numbers have minus one.

提交回复
热议问题