Help understanding what this line of PHP is doing - php

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.

Related

& as a Arithmetic Operator in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I am working on some legacy code where I came across the following function:
function is_odd($number) {
return $number & 1; // 0 = even, 1 = odd
}
I have never seen a method to check if a number is odd written like that before and am just trying to understand what they actually are doing.
& is a bitwise-and, it basically works so that for every bit that is 1 in both operands, it yields 1 in the resulting value and 0 for all other bits. If you convert any number to its bit representation, you quickly see that it's the lowest bit that determines whether a number is even or odd, for example:
5 = 101
10 = 1010
13 = 1101
1030 = 10000000110
The lowest bit (the one on the very right, also called the least significant bit) is 1 for every odd number and 0 for every even number. Doing $n & 1 will always yield 0 for every other bit than the lowest bit (because the number 1 only has one bit, you can imagine that the rest of the bits are left-padded with 0 to match the length of the other operand). So basically the operation boils down to comparing the lowest bit of the operands, and 1 & 1 is 1, all other combinations are 0. So basically when the $n & 1 yields 1, it means the number is odd, otherwise it's even.
EDIT.
Here's a few examples to demonstrate how the bitwise-and works for the example values I gave earlier, the number in the parenthesis is the original decimal number:
101 (5)
& 001 (1)
---
001 (1) = true
1010 (10)
& 0001 (1)
----
0000 (0) = false
1101 (13)
& 0001 (1)
----
0001 (1) = true
10000000110 (1030)
& 00000000001 (1)
-----------
00000000000 (0) = false
From this you can easily see that the result is only true when both operands' right-most bits are 1.

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...

PHP debug_backtrace bitmask usage

Trying to understand this entry in the php manual on debug_backtrace.
I don't understand what they mean by "this parameter is a bitmask for ...."
I have done web searches on bitmasks and my head is spinning round so I have decided I don't really want to learn the detail about it but just to know how I can supposed to add the options to that function.
Do I put in both options as in
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, DEBUG_BACKTRACE_IGNORE_ARGS)
if I want both and one of them if I only want that one?
Be aware that those 2 constants (DEBUG_BACKTRACE_PROVIDE_OBJECT, DEBUG_BACKTRACE_IGNORE_ARGS) are different in meaning. While DEBUG_BACKTRACE_PROVIDE_OBJECT provides an additional object when present, DEBUG_BACKTRACE_IGNORE_ARGS strips the args when present.
Since the most common use-case for these constants is to reduce memory usage, the way with least memory-consumption is:
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
NOT
// false friend!
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);
It overrides the default of DEBUG_BACKTRACE_PROVIDE_OBJECT and additionally ignores DEBUG_BACKTRACE_IGNORE_ARGS.
The constants will have values of 2^n in decimal, or (10)^n in binary. For example - 1, 10, 100, 1000, etc (in binary).
Say a=001, b=010, c=100:
You can do bitwise or on, for example, a and b. This will mean each bit will be 'turned on' if the same bit in either a or b is 'on'.
a | b == 011
This is a bitmask. The bitmask is checked for the inclusion of a by
bitmask & a != 0
Which is
011 & 001 == 001 != 0
However, because c is not in the bitmask:
bitmask & c == 011 & 100 == 0
So, to include both a and b in the bitmask, you use the binary or operator.
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);
It means you combine options with the bitwise OR operator: |.
For example:
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS);
For more details about bitmasks: http://en.wikipedia.org/wiki/Mask_(computing)

PHP if statement question (& operator)

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.

Categories