Comparing two integers without any comparison

前端 未结 14 1789
温柔的废话
温柔的废话 2020-12-05 16:06

Is it possible to find the greatest of two integers without any comparison? I found some solutions:

if(!(a/b)) // if a is less than b then division result          


        
14条回答
  •  再見小時候
    2020-12-05 16:33

    Subtract them and check the sign using nasty bit twiddling hacks
    http://graphics.stanford.edu/~seander/bithacks.html

    Don't do this in production code if the other programmers know where you live.

提交回复
热议问题