如何使用一些常数和业者不用乘法/分支翻转的整数值的符号 [英] How to flip the sign of an integer value using some constant and operator(s) without multiplication/branching

查看:240
本文介绍了如何使用一些常数和业者不用乘法/分支翻转的整数值的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个前pression这将使我具有以下属性写:

I am looking for an expression which would enable me to write with the following properties:

f(x, SOME_CONSTANT) -> returns -x (or any negative value)
f(x, SOME_CONSTANT2) -> returns x (or any positive value)
f(0, SOME_CONSTANT) -> returns 0
f(0, SOME_CONSTANT2) -> returns 0

不用乘法/分支,尽可能有效。

without multiplication/branching, as efficient as possible.

乍一看X ^ 0x80000000的似乎是一个候选人,但是当x为0这是行不通的。

At first glance x ^ 0x80000000 seems like a candidate, but it doesn't work when x is 0.

推荐答案

好吧,我终于想通了如何做到这一点有效的:

Ok, I finally figured out how to do this efficiently:

Java的:

int f(int x, int y) {
  return (((x >> 31) | ((unsigned)-x >> 31)) ^ y) - y;
}

C / C ++:

C/C++:

int f(int x, int y) {
  return (((x > 0) - (x < 0)) ^ y) - y;
}

以上这些函数返回 -sgn(X) y为 1 SGN(X)其他。

或者,如果我们只需要为每一个其他值,则-2 ^ 31(最小无符号整型值)工作,以preserving绝对值的利益,这是翻转的迹象,根据功能变量y:

Or, if we just need to work for every value other then -2^31 (minimum unsigned int value), with the benefit of preserving the absolute value, this is the function which flips the sign, depending on the variable y:

int f(int x, int y) {
    return (x ^ y) - y; // returns -x for y == -1, x otherwise
}

的推导
-x = =〜X + 1 = =(X ^ 0xFFFFFFFF的)+ 1 = =(X ^ -1)+ 1 = =(X ^ -1) - (-1)。代-1其中y,我们得到一个两可变式其具有返回如果y设置为0不变x一个有趣的性质,因为既没有(X ^ 0)也不减去0变化的结果。现在角球的情况是,如果x等于为0x8000000当这个公式不起作用。这可以通过应用的SGN(x)函数是固定的,所以我们有(SGN(x)^ Y) - Y)。最后,我们与著名的公式不使用分支代替SGN(x)的功能。

The derivation: -x == ~x + 1 == (x ^ 0xFFFFFFFF) + 1 == (x ^ -1) + 1 == (x ^ -1) - (-1). Substituting -1 with y, we obtain a two-variable formula which has an interesting property that returns unchanged x if y is set to 0, because neither (x ^ 0) nor subtracting 0 changes the result. Now the corner case is if x is equal to 0x8000000 when this formula doesn't work. This can be fixed by applying the sgn(x) function, so we have (sgn(x) ^ y) - y). Finally, we replace the sgn(x) functions with the well-known formulas which do not use branching.

这篇关于如何使用一些常数和业者不用乘法/分支翻转的整数值的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆