C:在无符号变量中执行有符号比较而不进行强制转换
我想要一个具有以下签名的函数:
bool signed_a_greater_than_signed_b(unsigned char a, unsigned char b);
当且仅当 a
中存储的位的 2 补码视图大于该位的 2 补码视图时,其输出应该为 1
存储在b
中。否则输出应为0
。例如:
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
该函数不进行任何隐式/显式转换(因为这些转换是 实现定义的,因此不可移植)
这样的实现之一是:
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));
}
您能建议一种使用算术而不是条件来执行此计算的实现吗?如果必须
在比较中混合使用无符号/有符号变量,则可以使用一个条件,而 C 中的算术会导致灾难。该函数是如何规避该问题的示例。
我猜签名变量比较背后的程序集与我想要实现的函数类似(在不支持签名比较的架构上)
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
. otherwise the output should be 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. 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 2 的补码:
其中
signbit
显然是表示形式的 MSB。Assuming 2's complement:
where
signbit
is obviously the MSB of the representation.您已经有了仅使用一种条件的解决方案。 ;)
由于您希望进行算术运算而不是条件,因此我认为目标是速度。使用查找表甚至比算术还要快。因为您使用的是 8 位字符,所以查找表并不意味着多余:您甚至不需要大小为 256x256 的表。表大小 256 完全足以存储
a
的每个值的限制,指示值b
可能必须导致 true(或 false)。每个函数调用只需要进行一次查表(a
->limit
)和一次比较(limit
<> <代码>b)。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. 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). Each function call only needs to perform one table look-up (a
->limit
) and one comparison (limit
<>b
).