PHP if statement question (& operator) - php

I'm using a function I found online. What does the & mean in this conditional?
if ($strength & 8) {
$consonants .= '##$%';
}
$strength is supposed to be a number 0-8. The function is intending to use all $consonants concatenations where $strength < 8. (might explain why the function is not working).

A single & is the bitwise operator and the double && is the logical. (i.e. Bits that are set in both $strength and 8 are set in your example.) It's a lot more complicated than just saying that and it requires an understanding of how binary works.
EDIT: Check out this article for more information on Bitwise operators.

& is a bitwise operator - it's checking to see if the bits that total 8 are set. In this case, 1000

& is a bitwise operator. It combines two values bitwise.
What is a bitwise operator?
Every integer is internally represented as a number of bits.
1 is 0001
2 is 0010
4 is 0100
8 is 1000
And so on. every bit's value is twice as big as the one preceding it.
You can get other numbers by combining bits
3 is 0011 (2+1)
5 is 0101 (4+1)
A bitwise operation works on every bit in both variables. & sets every bit in the result to 1 if it is 1 in both values it operates on.
9&5 == 1
because
9 == 1001
5 == 0101
----------
1 == 0001
| will COMBINE all 1s:
3|5 == 7
3 == 0011
5 == 0101
---------
7 == 0111
How can yo use it?
Example:
define('LOG_WARNING',1);
define('LOG_IO',2);
define('LOG_ALIENATTACKS,4);
$myLogLevel = LOG_WARNING | LOG_ALIENATACKS;
Now $myLogLevel is a combination of LOG_WARNING and LOG_ALIENATTACK. You can test it with the & operator:
if($myLogLevel&LOG_WARNING).... //true
if($myLogLevel&LOG_IO).... //false
if($myLogLevel&LOG_ALIENATTACKS)..../ /true run or your live!!!
If you want to know more about the topic search for bitflags and binary operations.

Related

What is $a & $b and $a | $b in php?

What happend $a & $b and $a | $b in php ?
$a = 11;
$b = 7;
echo $a & $b;
In the above php code it will give result 3 How it will calculate 3 ?
$a = 11;
$b = 7;
echo $a | $b;
In the above php code it will give result 15 How it will calculate 15?
Explain me both condition in php?
What's happening here is something called Bitwise Operators. It's more often used in much lower level languages, and it's one of the very simplest things a computer can do. Most computation is built on top of it. So what is it? Well, & is the Bitwise AND operator, and | is the Bitwise OR operator. You can test them with this online tool. But let's breakdown how it works.
AND Operator
Take two binary strings and any 1's that aren't in both binaries, the the same place, become a 0.
7 is 111
11 is 1011
So if you perform an AND on them, you get something like this
0111 &
1011 =
0011
0011 in Decimal is 3. You get 0011 because only the last two places are BOTH 1.
OR Operator
OR is basically the opsite. If a position in either binary is 1, then the output is 1. So when you perform it on 7 and 11, you'll get
1011 |
0111 =
1111
And 1111 is 15 in decimal
In binary, 11 is 00001011 and 7 is 00000111 (showing only relevant 8 bits for simplicity).
So 11 & 7 (bitwise AND)
00001011
00000111 & matching only where both matching bits are `1`
--------
00000011
which is the binary for 3
So 11 | 7 (bitwise OR)
00001011
00000111 | matching where either (or both) bits is `1`
--------
00001111
which is the binary for 15

Using two flags in split method in PHP. Why the | but not the &&? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I am trying to learn PHP and programming.
In the book I am studying there is something like:
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$flags is used for the split() method.
The first flag is: If this flag is set, only non-empty pieces will be returned by preg_split().
The second is: If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
Why isn't he using && operator but the | ?
Can you please explain what | does actually?
As far as I know these are
&& is the logical AND whereas | is a bitwise operator.
a && b evalutes to true when both operands are evaluted to true. Since flags are almost always numbers greater than 1, this expression always evalutes to true.
How functions accepting bitmasks work
They specify constants
Note that these constants have to have powers of 2:
FLAG_INT1 = 1
FLAG_INT2 = 2
FLAG_INT3 = 4
FLAG_INT4 = 8
You call the function combining some flags
myFunction(FLAG_INT1 | FLAG_INT3)
This leads to a bitwise OR operation:
0001
OR 0100
=======
0101
A set bit (1) in one (or both) of the operands will lead to a set bit (1) in the result, too.
The function checks internally for each flag
This requires a bitwise AND operation:
0101
AND 0001 // check for FLAG_INT1
========
0001 // true
0101
AND 0010 // check for FLAG_INT2
========
0000 // false
0101
AND 0100 // check for FLAG_INT3
========
0100 // true
The bitwise AND required both operands to have a set bit (1) at position X in order to result in a set bit (1) in the result at position X.
Wikipedia has also a nice article about the common bitwise operators: http://en.wikipedia.org/wiki/Bitwise_operation
&& is the Logical "AND"
but the | is a bitwise

Alternating table row styles in PHP - strange usage of bitwise operator

Looking at some code written by another developer, I came across this:
for($i=1; $i<=30; $i++)
{
if($i&1)
$color = '#fff';
else
$color = '#bbb';
}
This $color variable is used for row background colour later in the code. The alternating colours work fine.
If I was writing this, I would have used the modulus operator (%) rather than the bitwise (&) operator.
Why does the bitwise operator work in this case? Is there any advantage of using this method rather than the modulus operator?
The & operator does a bitwise comparison on the number. So if you do
$i & 1
it will then tell you if the '1' flag is set, such as in binary:
001010111010
The last number is the '1' flag (remember, binary goes 1, 2, 4, 8 etc. in reverse order), which in this case is set to 0.
Since 1 is the only odd flag in binary, it will tell you if the number is odd or even.
if $i is 3 for example, then in binary it will be 011 - the last number is a 1 (the 1 flag) and thus $i & 1 will be true.
if $i is 4 for example, then in binary it will be 100 - the last number is a 0 (the 1 flag) and thus $i & 1 will be false.
It works because the first bit is always 1 if the number is odd and 0 if the number is even.
1
10
11
100
101
110
111
etc.
In theory bitwise operation is faster than the modulus operation, but it's possible that the interpreter would have optimized the modulus operation down to bitwise operation anyway.
Why the other developer used it, we can only guess: out of habit, copy-pasted from somewhere, doesn't know about the modulus operator, showing off, wanting to optimize...

Help understanding what this line of PHP is doing

The variables values are listed below
$v['flag'] = 10
kPOSTAGE_HOME = 8
So what the heck does the following line do?!
if(($v['flag']&kPOSTAGE_HOME)==kPOSTAGE_HOME) {
//do something
}
& sets the bits set on both values. Some binary maths:
00001010 | 10
& 00001000 | 8
---------------
= 00001000 | 8
So 10&8 returns 8, and 8==8. Reason is to check whether a flag in that bit mask is set ...
It checks whether the bit-pattern in $v['flag'] has it's 3rd bit set.
And, for better readability, it can be simplified to the following:
if ( $v['flag'] & kPOSTAGE_HOME ) {
It's masking the '8' bit in the variable. The number '10' in base 10 == 1001 in binary, and 8 == 1000. So this means "does 1001 have the 1000" bit set?" The answer is 'yes'.
It checks whenever third bit is on in $v['flag'].
The & is "bitwise and" operator, binary of 8 is "00000100", therefore then you will do "bitwise and" all bits except the third will be zero, so in case third bit is on it will remains, therefore you have further check for equality.

What does the bitwise code "$n & ($n - 1)" do?

What does this code mean and what are other ways accomplish the same without using bit shifting?
if ($n & ($n - 1))
That formula checks to see whether a number is a power of 2 (if your condition as written is true, then the number is not a power of two).
Stated another way, your test checks to see whether there is more than one "1" bit set in the binary representation of $n. If there is zero or only one bit set, then your test will be false.
It is by far the most efficient way to determine that property.
First, this code is valid PHP, so your title is poor.
Second, the binary arithmetic going on looks something like this:
42 = 101010
&
41 = 101001
-----------
40 = 101000
Like Greg states this is the fastest way to check for a power of 2 number, but the code you've given checks to see if the number is not a power of 2. This can easily be ascertained by PHP's policy of: any non-null/non-zero value is true.
When we use ($n & ($n - 1)) then it converts $n & ($n-1) to its binary values and does binary AND operation.
Example
3 = 0011
4 = 0100
5 = 0101
3 = 0011
&
4 = 0100
------------
0
4 = 0100
&
5 = 0101
-----------
100
To check if given number is power of 2 or not we alway use formulae
($n & ($n - 1) == 0) which means ANDing of $n & $n-1 is equals to 0 or not.
Online AND Operation Calculator

Categories