是否可以在不使用shift或xor的情况下在x86中找到数字的最小值/最大值?

时间:2022-08-21 20:12:18

I'm trying to write min/max functions in x86. I've been able to do this with the use of shifts and with the use of xor, but I need to find a way to write it without these uses. Can someone lead me in the right direction for this?

我正在尝试在x86中编写min / max函数。我已经能够通过使用shift和使用xor来实现这一点,但我需要找到一种方法来编写它而不使用这些。有人能引导我朝着正确的方向前进吗?

1 个解决方案

#1


Example max function:

示例最大函数:

int max(int *arr,int n){
    int max=arr[0];
    for(int i=0;i<n;i++)max=(arr[i]>max)?arr[i]:max;
    return max;
    }

This will take an array of integers (arr) and it's size (n) as argument and return the maximum value containing in the array.

这将获取一个整数数组(arr)和它的size(n)作为参数,并返回包含在数组中的最大值。

To find min change max=(arr[i]>max)?arr[i]:max; to max=(arr[i]<max)?arr[i]:max;

找到最小变化max =(arr [i]> max)?arr [i]:max; to max =(arr [i] )?arr>

#1


Example max function:

示例最大函数:

int max(int *arr,int n){
    int max=arr[0];
    for(int i=0;i<n;i++)max=(arr[i]>max)?arr[i]:max;
    return max;
    }

This will take an array of integers (arr) and it's size (n) as argument and return the maximum value containing in the array.

这将获取一个整数数组(arr)和它的size(n)作为参数,并返回包含在数组中的最大值。

To find min change max=(arr[i]>max)?arr[i]:max; to max=(arr[i]<max)?arr[i]:max;

找到最小变化max =(arr [i]> max)?arr [i]:max; to max =(arr [i] )?arr>