Exactly that.. what is the & operator used for... not the && but &
Thanks
PHPdev
Exactly that.. what is the & operator used for... not the && but &
Thanks
PHPdev
stolzyboy,
Thanks for the link, I have already looked through that section, and read the comments. Thus I know it is a Refrence Operator, but I still dont understand what it does. If you could explain this, I would be most appreciative.
Thanks
PHPDev
no, it isn't a reference operator, it is a precedence operator, as in order of execution when adding, not sure of its power in the order, but it is used much like ()'s and such, like
(2 + 4) 5 + (7 8)
i'm sure you could figure out the order of exection, we learned this in simple math, now the & does the same thing i am just not sure of its precedence, the chart on that link says the lowest precedence are listed first so i am assuming that & has high precedence, it could probably be written like below, but i am not sure the order that would be executed i think it would come out the same way since * is lower on the chart than &, but it is higher than +/-
(2 + 4) 5 & (7 8)
stolzyboy,
So if it is precidence operator then What in the HECK does this do?
$x = 105
if ($x & 4) {
do this
}
I am reading through someone elses code, and this is the only statement I do not understand.
Thanks
PHPdev
& is a bit wise AND. What this means is that the AND is done in binary. So, let's play:
0 = 00000000
1 = 00000001
2 = 00000010
3 = 00000011
4 = 00000100
5 = 00000101
6 = 00000110
7 = 00000111
8 = 00001000
and so on.
So, let's bitwise and a few of these. An and function produces 1 for inputs of 1 and 1, and 0 for all else, so:
0 & anything = 0 (00000000 & xxxxxxxx)
1 & 2 = 0 (00000001 & 00000010)
2 & 3 = 2 (00000010 & 00000011)
5 & 13 = 5 (00000101 & 00001111)
127 & 13 = 13 (01111111 & 00001111)
So, that part of your code that has:
if ($x & 4) is really saying that anytime that the bit inside $x representing 4 is turned on, return true. If we look at the output, we see this:
0 to 3 & 4 = 0
4 to 7 & 4 = 4
8 to 11 & 4 = 0
12 to 15 & 4 = 4
and so on, so it switches on (or off) every time $x cycles four times.
Sxooter,
Thanks... that does make sense..
PHPdev