我可以在 Actionscript 3 中使用按位运算将负数转为正数吗?

发布于 2024-11-24 14:29:08 字数 306 浏览 1 评论 0原文

有没有直接的方法如何使用 Actionscript 3 中的按位运算将负数变成正数?我只是想我在某处读到过,这比使用 Math.abs() 或乘以 -1 更快。或者我错了,这是经过一天的学习字节和按位运算后的一个梦想?

我看到的是按位 NOT 几乎可以解决问题:

// outputs: 449
trace( ~(-450) );

如果有人发现这个问题并且感兴趣 - 在 500 万次迭代中 ~(x) + 1Math.abs(x)

Is there a direct way how to turn a negative number to positive using bitwise operations in Actionscript 3? I just think I've read somewhere that it is possible and faster than using Math.abs() or multiplying by -1. Or am I wrong and it was a dream after day long learning about bytes and bitwise operations?

What I saw was that bitwise NOT almost does the trick:

// outputs: 449
trace( ~(-450) );

If anyone find this question and is interested - in 5 million iterations ~(x) + 1 is 50% faster than Math.abs(x).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我很OK 2024-12-01 14:29:08

按位取反后需要加一。这是二进制补码系统的属性。它与 Actionscript 无关(除了所谓的性能差异)。

因此,(~(-450)+1) 给出 450
(~(450)+1) 给出 -450

正如评论中所指出的,这个答案是为了回答问题而写的,以解决提问者实验中的一个小问题。这个答案并不认可这种技术用于一般软件开发。

You need to add one after taking the bitwise negation. This is a property of two's complement number system. It is not related to Actionscript (aside from the alleged performance difference).

So, (~(-450)+1) gives 450
and (~(450)+1) gives -450.

As noted in comments, this answer is written in response to the question, to fix a minor issue in the question asker's experiment. This answer is not an endorsement of this technique for general software development use.

以可爱出名 2024-12-01 14:29:08

使用以下规则

~(x) = (-x)-1

Use the rule that says

~(x) = (-x)-1
迷乱花海 2024-12-01 14:29:08

如果使用二补码(通常是这种情况),则否定是补码然后加 1:

-x == ~x + 1

是否更快取决于编译器执行的优化。如有疑问,请进行测试。

If two-complement is being used (usually the case), negation is complement then add 1:

-x == ~x + 1

Whether it's faster depends on what optimisations the compiler performs. When in doubt, test.

败给现实 2024-12-01 14:29:08

否定本身就是一个运算符,即一元 - 运算符。使用它与使用按位运算一样快,并且可以节省大量输入。

negativeX = -positiveX; // is the same as (~positiveX) + 1

不执行乘法。

如果您需要速度,并且您不知道数字是负数还是正数,那么三元运算符 ?: 比引入 Math.abs() 的函数调用开销更快

positiveX = unknownX < 0 ? -unknownX : unknownX;

Negation is an operator all unto itself, the unary - operator. Using this is just as fast as using bitwise operations and saves you a lot of typing.

negativeX = -positiveX; // is the same as (~positiveX) + 1

No multiplication is performed.

If speed is your need, and you don't know if the number is negative or positive, the ternary operator ?: is faster than introducing the function-call overhead of Math.abs().

positiveX = unknownX < 0 ? -unknownX : unknownX;
慕巷 2024-12-01 14:29:08

基本上,数字 2' 的补码是相反符号的数字。

if (num < 0) {
   PosNum = ~num + 1;
}
else {
   NegNum = ~num + 1;
}

Basically numbers 2' complement is the number in opposite sign.

if (num < 0) {
   PosNum = ~num + 1;
}
else {
   NegNum = ~num + 1;
}
音盲 2024-12-01 14:29:08

试试这个:

var number:Number = 10;
//Makes a number
trace(number)
//Tells you the number BEFORE converting
number = number - number * 2;
//Converts number
// Takes number times 2 and subtracts it from original number
trace(number);
//Tells you the number AFTER converting

最后,你所需要的就是这个:

var number:Number = 10;
number = number - number * 2;

Try this:

var number:Number = 10;
//Makes a number
trace(number)
//Tells you the number BEFORE converting
number = number - number * 2;
//Converts number
// Takes number times 2 and subtracts it from original number
trace(number);
//Tells you the number AFTER converting

In the end, all you need is this:

var number:Number = 10;
number = number - number * 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文