什么是〜位运算符的功能(波浪号) [英] What's the function of the ~ bitwise operator (Tilde)

查看:852
本文介绍了什么是〜位运算符的功能(波浪号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  这是什么〜运算符的意思是在这里吗?结果
  <一href=\"http://stackoverflow.com/questions/8785054/bit-not-operation-in-phpor-any-other-language-probably\">Bit在PHP中没有操作(或任何其他语言可能)


有人可以解释我在PHP中的运营商?我知道这是一个 NOT-运营商的,但为什么PHP转换下面的语句来负变量减一的价值?

  $ a = 1;回声〜$一个//回声-2
$ A = 2;回声〜$ //一-3回声
$一= 3;回声〜$一个//回声-4


解决方案

这就是所谓的的补算术的。你可以阅读关于它的详细这里

运营商是一个二元反运算符(而不是布尔非),以及在于,它逆操作数的所有位。其结果是在二的补码算术负数。

Possible Duplicate:
What does this ~ operator mean here?
Bit not operation in PHP(or any other language probably)

Can someone explain me the ~ operator in PHP? I know it's a NOT-operator, but why does PHP convert following statement to the negative value of the variable minus one?

$a = 1; echo ~$a    // echo -2
$a = 2; echo ~$a    // echo -3
$a = 3; echo ~$a    // echo -4  

解决方案

This is called the two's complement arithmetic. You can read about it in more detail here.

The operator ~ is a binary negation operator (as opposed to boolean negation), and being that, it inverses all the bits of its operand. The result is a negative number in two's complement arithmetic.

这篇关于什么是〜位运算符的功能(波浪号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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