MIN and MAX in C

后端 未结 14 1555
攒了一身酷
攒了一身酷 2020-11-22 13:32

Where are MIN and MAX defined in C, if at all?

What is the best way to implement these, as generically and type safely as possible? (Compil

14条回答
  •  日久生厌
    2020-11-22 14:30

    If you need min/max in order to avoid an expensive branch, you shouldn't use the ternary operator, as it will compile down to a jump. The link below describes a useful method for implementing a min/max function without branching.

    http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax

提交回复
热议问题