了解PHP和放大器; (符号,按位与)运算符 [英] Understanding PHP & (ampersand, bitwise and) operator

查看:136
本文介绍了了解PHP和放大器; (符号,按位与)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用($ VAR和放大器; 1)在我的code,它返回true,如果 $ VAR 是奇数和虚假如果它是一个偶数。

I often use ($var & 1) in my code, which returns true if $var is an odd number and false if it's an even number.

但什么是&放大器;实际上做?

But what does "&" actually do?

推荐答案

&安培;是二进制的。如果你有一个二进制值,您与另一个二进制值,那么结果将是按位的他们俩。举个例子:

& is binary and. If you have a binary value, and you and with another binary value, then the result will be the bitwise and of the two. An example:

  01101010
& 01011001
= 01001000

最右边的位可以是一个1(在此情况下的数量是奇数),或者它是一个0,在这种情况下的数量是偶数。如果您&安培; 1的数字,你只看至少显著位,而如果检查的数量是1或0。正如其他人所说,看位运算符的信息他们是如何工作的。

The rightmost bit is either a 1 (and in that case the number is an odd number) or it is a 0, in which case the number is even. If you & a number with 1, you only look at the least significant bit, and the if checks if the number is a 1 or a 0. As others have mentioned, look at the bitwise operators for info on how they work.

这篇关于了解PHP和放大器; (符号,按位与)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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