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
Related
I have the following specification of bitmask values:
// Structure of Database:
// SC_NAME, flag
//
// flag 1 - SC cannot be removed by death.
// 2 - SC cannot be saved.
// 4 - SC cannot be reset by dispell.
// 8 - SC cannot be reset by clearance.
// 16 - SC considered as buff and be removed by Hermode and etc.
// 32 - SC considered as debuff and be removed by Gospel and etc.
// 64 - SC cannot be reset when MADO Gear is taken off.
// 128 - SC cannot be reset by 'sc_end SC_ALL' and status change clear.
// 256 - SC can be visible for all players
Here's an example usage of the bitmask:
SC_ENDURE, 21
The above means:
SC_ENDURE: cannot be removed by death and dispel and considered as buff. (16 + 4 + 1 = 21)
I have a CSV list (trimmed for example) to check, which looks like this:
SC_PROVOKE, 32
SC_ENDURE, 21
SC_HIDING, 4
SC_CLOAKING, 6
SC_TWOHANDQUICKEN, 24
SC_CONCENTRATION, 16
SC_ENCHANTPOISON, 16
SC_ORCISH, 2
What I want to do is go through the list select all effects that are considered as buff 16 into one list and the others into a separate list.
Using the example above; how do you check, if 16 exists in the sum of bit masks 21?
This is what I tried so far (with my lack of knowledge about bitmask) and not having any luck:
<pre>
<?php
$buff_list = [];
$not_buffs = [];
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
list ($effect_code, $bitmask_value) = $data;
$effect_code = trim($effect_code);
$bitmask_value = (int)trim($bitmask_value);
if (16 | $bitmask_value) {
$buff_list[] = $effect_code;
} else {
$not_buffs[] = $effect_code;
}
}
fclose($handle);
}
print_r($buff_list);
echo "<hr>";
print_r($not_buffs);
The code I tried is putting all effects into $buff_list, I am not sure if I am doing this right.
Replace
(16 | $bitmask_value)
with
(16 & $bitmask_value)
Edit to help clarify:
(16 | $bitmask_value) = All of the flags in &bitmask_value as well as 16.
Example: (1 | 16) = 17, ((4 | 16) | 16) = (4 | 16) = 20
(16 & $bitmask_value) = All of the flags in the &bitmask_value also in 16.
Example: (1 & 16) = 0, ((4 | 16) & 16) = 16, ((1 | 2 | 4) & (2 | 4 | 8)) = (2 | 4) = 6
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.
Can anybody help me to understand, how the following code works ? I know it will return 1 if for odd number and 0 for even number.
echo (7 & 1); // result 1
echo (6 & 1); // result 0
I think the numbers are converted to its binary. Please correct if I'm incorrect.
7 = 0000111b
1 = 0000001b
------------
& = 0000001b = 1
And for 6:
6 = 0000110b
1 = 0000001b
------------
& = 0000000b = 0
Yes, you are performing a AND operation on the numbers, so
Dec BINARY Output
7 === 0111
1 === 0001
------------------------
AND op 0001 1
Dec BINARY
6 === 0110
1 === 0001
------------------------
AND op 0000 0
Like Wise,
Dec BINARY
7 === 0111
6 === 0110
------------------------
AND op 0110 6
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Understanding PHP's & operator
I was just looking at array_filter() function doc and they had the following code to return odd numbers...
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
?>
Why does $var & 1 return odd number? how does that work?
& is bitwise and. It acts as a mask on the bits of $var. All odd numbers end with 1
no bit &1
1 001 1
2 010 0
3 011 1
4 100 0
5 101 1
6 110 0
7 111 1
You are using a bitwise function with always returns 1 when anded with an odd number.
A few examples:
11 = 3
01 = 1
----
01 = odd -- return 1 (true)
100 = 4
01 = 1
-----
000 = even -- return 0 (false)
One more:
10101 = 21
01 = 1
-------
00001 = odd -- return 1 (true)
That function return 1 if var is an odd number, 0 otherwise. "&" is the AND binary operator, so it considers the last binary digit of a number.
For example:
5 in binary is 101 -> 101 & 1 = 1 -> odd number.
8 in binary is 1000 -> 1000 & 1 = 0 -> even number.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Below is the PHP code:
<?php
$a = $b['key'] | 0;
?>
Is it an operator?
The | is the bitwise OR operator.
Doing a bitwise OR with zero (| 0) doesn't make any sense though, as it will not flip any bits. Maybe the guy who wrote this was just a really bad programmer and tried to cast a string to an integer that way. He should have used a (string) cast instead!
Its bitwise OR, as defined here.
For example:
Bitwise Inclusive OR
( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)
| is a bitwise OR function.
|| is a regular OR
It is the bitwise OR operator.
See here for more details:
http://php.net/manual/en/language.operators.bitwise.php
It is performing a bitwise OR operation.
It is possible the original author of the code meant to put another | in to make it a logical OR (||), because a bitwise OR with 0 will have no effect whatsoever on the output. Although even this makes no sense, he could have simply cast to an integer to get the same result.
| is a bitwise operator (http://php.net/manual/en/language.operators.bitwise.php)
| means "Bits that are set in either $b['key'] or 0 are set."
Because the second part is a zero though, it will return false only if $b['key'] is zero too.