[英]C: performing signed comparison in unsigned variables without casting
i want a function with the following signature: 我想要具有以下签名的功能:
bool signed_a_greater_than_signed_b(unsigned char a, unsigned char b);
its output should be 1
iff the 2's-complement view of the bits stored in a
is greater than the 2's complement view of the bits stored in b
. 如果
a
存储的位的2的补码视图大于b
存储的位的2的补码视图,则其输出应为1
。 otherwise the output should be 0
. 否则输出应为
0
。 for example: 例如:
signed_a_greater_than_signed_b(0b10000000,any number) => 0
signed_a_greater_than_signed_b(0b01111111,any number other than 0b01111111) => 1
signed_a_greater_than_signed_b(0b00000000,0b00000001) => 0
signed_a_greater_than_signed_b(0b00000000,0b11111111) => 1
signed_a_greater_than_signed_b(0b00000000,0b00000000) => 0
the function is not to have any implicit/explicit conversions (as these conversions are implementation-defined, and thus not portable) 该函数不应进行任何隐式/显式转换 (因为这些转换是实现定义的,因此不可移植)
one such implementation is: 一种这样的实现是:
bool signed_a_greater_than_signed_b(unsigned char a, unsigned char b)
{
// if 'signed' a is positive then
// return 1 if a is greater than b or b is negative
// otherwise, if 'signed' a is negative then
// return 1 if a is greater than b and b is negative
if (a <= 0b01111111) return ((b < a) || (b > 0x01111111));
else return ((b < a) && (b > 0x01111111));
}
can you suggest an implementation that uses arithmetic rather than conditionals to perform this calculations? 您可以建议使用算术运算而不是条件运算的实现吗? you may use one condition if you must
如果必须,可以使用一种条件
using a mix of un/signed variables in comparisons and arithmetic in C is a recipe for disaster. 在C语言中使用无符号/有符号变量的混合进行比较和算术是灾难的根源。 this function is an example of how to circumvent the problem.
此功能是如何解决此问题的示例。
i guess the assembly behind comparison of signed variables is similar to the function i want to implement (on architectures not supporting signed comparisons) 我猜想有符号变量比较后面的程序集类似于我要实现的功能(在不支持有符号比较的体系结构上)
Assuming 2's complement: 假设2的补码:
return (a^signbit) > (b^signbit);
where signbit
is obviously the MSB of the representation. 其中
signbit
显然是表示形式的MSB。
you may use one condition if you must
如果必须,可以使用一种条件
You already have a solution using only one condition. 您已经有了仅使用一个条件的解决方案。 ;)
;)
As you would like to have arithmetic operations rather than conditionals, I assume that the goal is speed. 因为您希望使用算术运算而不是条件运算,所以我认为目标是速度。 And using a look-up table is even faster than arithmetic.
使用查找表甚至比算术更快。 Because you are using 8 bit chars, a look-up table means no overkill: You don't even need a table of size 256x256.
因为您使用的是8位字符,所以查找表并不意味着多余:您甚至不需要大小为256x256的表。 A table size of 256 is perfectly adequate storing a limit for each value of
a
indicating the value(s) b
may have to result in true (or false). 256的表的大小是完全足够存储用于的每个值的限制
a
指示值(S) b
可能有导致真(或假)。 Each function call only needs to perform one table look-up ( a
-> limit
) and one comparison ( limit
<> b
). 每个函数调用仅需要执行一个查表(
a
- > limit
)和一个比较( limit
<> b
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.