fastest method for minimum of two numbers
Posted
by
user85030
on Stack Overflow
See other posts from Stack Overflow
or by user85030
Published on 2012-09-02T09:34:59Z
Indexed on
2012/09/02
9:38 UTC
Read the original article
Hit count: 152
Performance
|boolean-operations
I was going through mit's opencourseware related to performance engineering.
The quickest method (requiring least number of clock cycles) for finding the minimum of two numbers(say x and y) is stated as:
min= y^((x^y) & -(x<y))
The output of the expression x < y can be 0 or 1 (assuming C is being used) which then changes to -0 or -1. I understand that xor can be used to swap two numbers.
Questions: 1. How is -0 different from 0 and -1 in terms of binary? 2. How is that result used with the and operator to get the minimum?
Thanks in advance.
© Stack Overflow or respective owner