This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Can someone tell me what's the << 0 for? and other more common alternatives if they exist
$newvalue += 1 << 0;
<< is the bitwise left-shift operator.
In this case is seems pretty pointless, since left-shifting 1 by 0 equals 1.
<< 0 does nothing. It is probably there to indicate that the value is some sort of flag. If it was something other than 0, than it would shift the value (1) left by x bits, where is is the value replacing 0.
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What does the percent sign mean in PHP?
(6 answers)
Closed 3 years ago.
I just started to learn PHP deeper for Zend PHP certification and I found this code, which actually works. Can someone explain me the logic behind this?
<?php
$num = 20% - 8;
echo $num; // 4
What you're seeing is the modulus operator, which in essence asks "What is the remainder of 20 divided by -8".
So you might ask, why isn't it negative 4? From the manual,
The result of the modulo operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.
See the PHP: Arithmetic Operators for more documentation.
% is not per cent, but modulus, cf. https://en.m.wikipedia.org/wiki/Modular_arithmetic
20 = 2 * 8 + 4, therefore, 20 % 8 = 4
This question already has answers here:
int variable with leading zero?
(5 answers)
Closed 5 years ago.
What will be the output of this expression ?
$a = 012;
$b = $a / 2;
var_dump($b);
I got the answer (int) 5 Please anyone suggest me how this is calculate ?
When you use a leading 0 in 012, you're actually using octal notation. That means, that this is 12 number but as an octal number.
So012 is actually 8+2, which is 10 as a decimal number.
This question already has answers here:
Understanding PHP & (ampersand, bitwise and) operator
(5 answers)
Closed 7 years ago.
i'm new here and i have a question that i couldn't find an answer to:
<?php
$val1 = 25;
$val2 = 15;
echo ($value & $value1); // output : 9
?>
Can anyone explain step by step how this returned 9 ?
Thank you
& is an AND operator. It's sort of like &&, but applies to binary numbers.
25 = 00011001
15 = 00001111
.....& 00001001 which in binary is 9
Basically, only the bits that are 1 in both the first and second number remain 1, the rest turn to 0.
& operator works with binary representation of a number:
25 is 00011001
15 is 00001111
00011001
&
00001111
--------
00001001
Which is 9 in decimal.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
The following code uses a single & in a conditional check. What does the single ampersand mean there?
if( $some_array_or_other_var & SOME_CONSTANT_VARIABLE ){
//do something here
}
It does not look like a reference, that's what confuses me.
That is a bitwise AND operation: http://www.php.net/manual/en/language.operators.bitwise.php
If, after the bitwise AND, the result is "truthy", the clause will be satisfied.
For example:
3 & 2 == 2 // because, in base 2, 3 is 011 and 2 is 010
4 & 1 == 0 // because, in base 2, 4 is 100 and 1 is 001
This is commonly used to check a single bit in a bitset, by testing powers of two, you are actually checking if a specific bit is set.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I've been trying to get a code together that would deduce what alternatives have been check in a multi-choice exam, but using sums of powers of two.
I found the following code online, but I'm not sure HOW it works, especially the operator <<. I could not find this on Google or PHPDoc.
Thank you.
$aSums = array();
for ($iCount = 0; $iCount < 32; $iCount++)
{
$iMask = 1 << $iCount;
if (($iNumber & $iMask) != 0)
$aSums[] = $iMask;
}
return $aSums;
It's a bitwise shift left, that is, multiply by 2^N.
It's a bitwise operator Shift left. From PHP documentation:
Shift the bits of $a $b steps to the left (each step means "multiply by two")