简体   繁体   English

如何在没有相等运算符的情况下区分两个整数?

[英]How to differentiate between two integers without equality operators?

Given two integers, how can I differentiate between the two integers, maximum, minimum and equality without using if statements or selections (>=, >, and =). 给定两个整数,如何在不使用if语句或选择(> =,>和=)的情况下区分两个整数(最大值,最小值和相等)。 I thought about using fmin and fmax , but I also need to know how to know if the two integers are the same. 我考虑过使用fminfmax ,但是我还需要知道如何知道两个整数是否相同。

Assuming 2's complement arithmetic: 假设2的补码算法:

int intcmp(int int1, int int2)
{
    int diff=int2-int1;
    unsigned int udiff;
    memcpy(&udiff, &diff, sizeof(diff));
    if(!udiff)
        return 0;  /* the two integers are equal */
    else if(udiff & 1<<(sizeof(udiff)*CHAR_BIT-1))  /* check the sign */
        return +1;  /* int1 < int2 */
    else
        return -1;  /* int2 < int1 */
}

怎么样...

(num1-num2) ? ((num1-num2) & (MAX_INT+1) ? NEGATIVE : POSITIVE) : EQUAL

What do you mean by "differentiate"? “区别”是什么意思? You can't cause two numbers to select two different execution paths without some sort of branch operation, but you could in theory load an array with function pointers and manipulate the numbers to select different array elements to call (and a call is a form of branch). 您不能在没有某种分支操作的情况下使两个数字选择两个不同的执行路径,但是从理论上讲,您可以使用函数指针加载数组并操纵数字以选择要调用的不同数组元素(而调用是一种形式的科)。

You could somewhat more easily load an array with two different character string pointers, and select which to print based on which number you get. 您可以更轻松地加载带有两个不同字符串指针的数组,然后根据获得的数字选择要打印的数组。

You can convert a number to 0 or 1 based on whether it's odd or even by ANDing with 1, eg. 您可以根据数字是奇数还是偶数与1进行与运算,将数字转换为0或1。 You can determine if numbers are equal by subtracting them and seeing if you get zero or not. 您可以通过减去数字并确定是否为零来确定数字是否相等。 Etc. 等等。

You could use the following to generate a mask 您可以使用以下内容生成遮罩

signed int x = ...;
signed int y = ...;
mask = (x - y) >> 31;

So mask is -1 if y>x, and is 0 otherwise (but be careful of overflow). 因此,如果y> x,则mask-1 ,否则为0 (但要注意溢出)。 You can then use & , | 然后可以使用&| , and ~ to build your result using this mask. ,然后~使用此掩码构建结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不使用任何算术运算符将两个整数相乘 - Multiply two integers without using any arithmetic operators 如何在不使用任何比较运算符和不使用 if、else 等的情况下以编程方式返回两个整数的最大值? - How do I programmatically return the max of two integers without using any comparison operators and without using if, else, etc? 处于相同“if”条件的两个“==”相等运算符未按预期工作 - Two '==' equality operators in same 'if' condition are not working as intended 如何用C语言-Loadrunner Web区分两个日期时间? - How to differentiate between two Date Time in C language -Loadrunner Web? 如何区分 RTOS 和 Linux? - How to differentiate between an RTOS and Linux? 如何从c中的字符串中分离出整数和运算符? - how to separate integers and operators from a string in c? 比较两个内存块之间的值相等 - Compare equality of the values between two blocks of memory 如何计算MIPS中两个值之间的偶数整数之和? - How to calculate the sum of even integers between two values in MIPS? 我如何输入两个整数,中间用破折号? - How do I input two integers with a dash in between? 如何使用C将两个整数合并为一个字符串而无需malloc? - How to combine two integers into one string without malloc using C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM