This question already has answers here:
PHP Operator <<
(5 answers)
Closed 7 years ago.
I am not clearly understand about the following code snippets.
$a = (5 << 0);
$b = (6 << 1);
echo $a|$b;
From php.net i knew that << operator use for shift left but not clear how it works and what is the uses of | operator.
Any explanation is highly appreciated.
Thank you
5 << 0 produces just 5, since no shift is done. 6 << 1 will shift the bits in 6 (110b) one to the left, which will produce 12 (1100b). So it is multiplying by two essentially.
The | operator is bitwise or, which operates on the bits of 5 (0101b) and 12 (1100b) producing 13 (1101b)
6 is '110' in binary. If you shift '110' once to the left you get '1100' which is 12
<< is the bitwise shift left operator:
00000110 is 6 in binary
6 << 1 means that each bit will be shifted 1 to the left:
00000110 (6)
becomes
00001100 (12)
so... 6 << 1 = 12
5 << 0 makes no difference as none of the bits are being shifted (5 << 0 = 5).
| is the bitwise 'or' operator:
5|12 makes:
00000101 | 00001100
This operator means if both bits are 0, the result will be 0, otherwise 1:
00000101 (5)
00001100 (12)
00001101 (13)
So 5|12 = 13
Hope you can find the solution here
Strange print behaviour in PHP?
for more information, you can check this link
Reference - What does this symbol mean in PHP?
Related
This question already has answers here:
What are bitwise shift (bit-shift) operators and how do they work?
(10 answers)
Closed 4 years ago.
$a = 23;
$res = $a << 4
print($res);
For the code snippet pasted above, the output is 368. How is it being calculated?
I expected 92.
a is 23
a in base 2 is 10111
so a in 8 bits is 00010111
Left shift 4 is 01110000
It is - 92
Can some body explain me ?
This is why it is returning 368 because $a << $b Shift the bits of $a $b steps to the left
23
256 128 64 32 16 8 4 2 1
X X X X 1 0 1 1 1
after $a << 4
368
256 128 64 32 16 8 4 2 1
1 0 1 1 1 0 0 0 0
Check bitwise operator manual here
Convert 23 in binary and it is 10111 and when you perform shift left bitwise operator, its value will be 101110000 and when you convert it to decimal it becomes 368.
Can somebody explain how this works? Thanks a lot :)
define("FLAG_A", 1);
define("FLAG_B", 2);
define("FLAG_C", 4);
function test_flags($flags=false) {
if ($flags & FLAG_A) echo "A";
if ($flags & FLAG_B) echo "B";
if ($flags & FLAG_C) echo "C";
}
test_flags(FLAG_A|FLAG_B);
This is done using what is called bitwise math. Specifically, they are using the & (bitwise and) operation to check to see if a value is set. For the sake of argument here, we will use 4-bit numbers, to keep it short and simple.
The number 7, in 4-bit binary, is as follows:
0111
Each digit has a value that is doubled each time, and all of them are added together to make the total number of 7. Broken down, it works like so:
0 1 1 1
^ ^ ^ ^
| | | |
8 4 2 1
So, essentially, it is:
0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 = 7
Now, with bitwise math, specifically the bitwise and, we can say that we only care about bits in certain columns -- basically, the bit in each column must be 1, or it will be set to zero. So, checking for '4' inside '7' with bitwise math:
0111 (7)
& 0100 (4)
______
0100 (4)
Since this value is non-zero, it is true, and we can determine that the value 4 is inside the value 7. Now consider the number 11:
1 0 1 1
^ ^ ^ ^
| | | |
8 4 2 1
1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 = 11
Try looking for 4 in that
1011 (11)
& 0100 (4)
______
0000 (0)
Since it has a value of 0, it is false, and we can consider that the number (4) is not inside the number 11.
Similarly, we can conclude that numbers 4, 2, and 1, but not 8, are in 7. In 11, we have 8, 2, and 1. By treating numbers as a series of bits, instead of a singular value, we can store many flags inside a single integer.
Definitely do some more reading on bitwise math, it can be very useful when used correctly, just don't try to shoe-horn it into everything.
This is based on bitwise operators. Think of
define("FLAG_A", 0001); // in binary form
define("FLAG_B", 0010);
define("FLAG_C", 0100);
Hence, e.g., $flags & FLAG_A is a bitwise AND between the variable $flags and the constant FLAG_A.
<?php
echo 2<<3; //Output 16
echo '---';
echo 3<<2; //Output 12
?>
Tried to find out logic. But its ends up in vain!! Can someone explain it please
The << operator is a bitwise operator. This basically means the numbers are treated as binary numbers, and the interaction is about moving bits around.
So let's have a look at the numbers and the operations:
First, 2 << 3
0b000010 // 2 in binary
0b010000 // move the bits three left, we get 16
Then 3 << 2
0b000011 // 3 in binary
0b001100 // move the bits two left, we get 12
From the manual page linked above:
Shift the bits of $a $b steps to the left (each step means "multiply by two")
So 3<<2 in effect means 3*(2^2), while 2<<3 means 2*(2^3).
The << operator is bitwise left shift.
Let's write the numbers in their binary representation
0000 0010 // 2
0000 0011 // 3
And then shift them by 3 and 2 respectively:
0001 0000 // 16
0000 1100 // 12
2 = 0b10
0b100 = 4
0b1000 = 8
0b10000 = 16
3 = 0b11
0b110 = 6
0b1100 = 12
The fist operator (<<) is the bitwise shift operator, specifically the left-shift operator. It takes the left hand argument and shifts the binary representation to the left by the number of bits specified by the right argument. Right-shift (>>) does the same but to the right. Read more about it here http://php.net/language.operators.bitwise
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I googled much for finding answer but i couldn't find any.
I saw in few free source codes that they use "|" sign to combine values or something. can someone enlighten what is different between "|" and "+"?
sample:
<?php
define('PERMISSION_DENIED', 0);
define('PERMISSION_READ', 1);
define('PERMISSION_ADD', 2);
define('PERMISSION_UPDATE', 4);
define('PERMISSION_DELETE', 8);
$read_only = PERMISSION_READ;
$read_delete = PERMISSION_READ | PERMISSION_DELETE;
$full_rights = PERMISSION_DENIED | PERMISSION_READ | PERMISSION_ADD | PERMISSION_UPDATE | PERMISSION_DELETE;
$myrights = PERMISSION_READ;
$myrights |= PERMISSION_UPDATE;
?>
why not just:
<?php
define('PERMISSION_DENIED', 0);
define('PERMISSION_READ', 1);
define('PERMISSION_ADD', 2);
define('PERMISSION_UPDATE', 4);
define('PERMISSION_DELETE', 8);
$read_only = PERMISSION_READ;
$read_delete = PERMISSION_READ + PERMISSION_DELETE;
$full_rights = PERMISSION_DENIED + PERMISSION_READ + PERMISSION_ADD + PERMISSION_UPDATE + PERMISSION_DELETE;
$myrights = PERMISSION_READ;
$myrights += PERMISSION_UPDATE;
?>
it's the bitwise or operator, so you're operating with numbers in base 2 notation.
for example:
1000 | 0001 = 1001 ==> (8 | 1 = 9)
http://www.php.net/manual/en/language.operators.bitwise.php
In your code each permission is represented by one bit in a different position, I mean:
1 = 0001 = perm1
2 = 0010 = perm2
4 = 0100 = perm3
8 = 1000 = perm4
so doing or with those numbers gives you the number with all the permissions together. then if you wanna check if a permission is set you gotta do an and operation with the permission that you're checking, for example:
$user_perms = perm1 | perm3;
if ($user_perms & perm4) echo "user does not have this permission";
Okay, there is a difference. In order to see the difference you can just see how these numbers appear in binary:
0 = 00000000
1 = 00000001
2 = 00000010
4 = 00000100
8 = 00001000
As you can see, each of these numbers have only one bit set to one each in a different position.
Now, having a bitwise OR between them will make the result with one on each position as the operands:
00000010 |
00000100
----------
00000110
In this case it's the same as just adding the numbers.
0 | 0 = 0; 0 + 0 = 0
0 | 1 = 1 | 0 = 1 + 0 = 1
The difference comes here:
1 | 1 = 1
while
1 + 1 = 10 !!
The difference in this example is that bit-wise operator is faster because you just operate on the bits.
| is the bitwise OR (every bit of the result is 1 iff at least one of the corresponding bits in the inputs is 1). Using addition would work as well, but doesn't make it clear one is assembling a bitmask, and can lead to bugs, because
PERMISSION_READ + PERMISSION_READ != PERMISSION_READ
but
PERMISSION_READ | PERMISSION_READ == PERMISSION_READ
This is a bitwise OR operator.
You can find more information about it here : http://php.net/manual/en/language.operators.bitwise.php
Bitwise operators usual are good for soft encription methods
What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
It's the modulus operator, as mentioned, which returns the remainder of a division operation.
Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.
5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.
10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.
In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.
It is the modulus operator:
$a % $b = Remainder of $a
divided by $b.
It is often used to get "one element every N elements". For instance, to only get one element each three elements:
for ($i=0 ; $i<10 ; $i++) {
if ($i % 3 === 0) {
echo $i . '<br />';
}
}
Which gets this output:
0
3
6
9
(Yeah, OK, $i+=3 would have done the trick; but this was just a demo.)
It is the modulus operator. In the statement $a % $b the result is the remainder when $a is divided by $b
Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:
<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>
% means modulus.
Modulus is the fancy name for "remainder after divide" in mathematics.
(numerator) mod (denominator) = (remainder)
In PHP
<?php
$n = 13;
$d = 7
$r = "$n % $d";
echo "$r is ($n mod $d).";
?>
In this case, this script will echo
6 is (13 mod 7).
Where $r is for the remainder (answer), $n for the numerator and $d for the denominator. The modulus operator is commonly used in public-key cryptography due to its special characteristic as a one-way function.
Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.
In mathematics, the remainder is the amount "left over" after
performing some computation. In arithmetic, the remainder is the
integer "left over" after dividing one integer by another to produce
an integer quotient (integer division).
See: http://en.wikipedia.org/wiki/Remainder
So % (integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"
To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1:
It can be broken down into:
(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1
7 can go into 8 1 time with .14 of 7 leftover
That's all there is to it. I hope this helps to simplify how exactly modulus works.
Additional examples using different divisors with 21.
Breakdown of 21 % 3 = 0:
21 / 3 = 7.0
3 * 0 = 0
(3 can go into 21 7 times with 0 of 3 leftover)
Breakdown of 21 % 6 = 3:
21 / 6 = 3.5
.5 * 6 = 3
(6 can go into 21 3 times with .5 of 6 leftover)
Breakdown of 21 % 8 = 5:
21 / 8 = 2.625
.625 * 8 = 5
(8 can go into 21 2 times with .625 of 8 leftover)