gandarez
gandarez

Reputation: 2662

Get Minimum value from 2 unsigned int

I'm trying to implement a function that have to return the minimum value from a list of array, without comparers (“==”, “!=”, “>”, “<”, “>=”, “<=”), but to simplify I will work with only two variables. Suppose I have two values : the number 5 declared as "a" and the number 35 declared as "b", so I found a way to get the minimum of two integers, but not two unsigned integers, see:

b + ((a - b) & ((a - b) >> 31));

Can anyone help me?

Upvotes: 0

Views: 1264

Answers (2)

user1986256
user1986256

Reputation: 11

Type casting is a costly operation. We want to calculate minimum using bit-wise ope

Upvotes: 1

Laurion Burchall
Laurion Burchall

Reputation: 2853

Cheap & cheesy solution: If you are using 32-bit unsigned integers you can cast them to 64-bit signed integers and use the code you have above.

Upvotes: 3

Related Questions