-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitwise.php
72 lines (57 loc) · 1.59 KB
/
bitwise.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
ini_set('display_errors', 1);
$user_perms = 0; // |0|0|0|0|0|1|1|1|
$perms = array(
'can_post' => 1, // |0|0|0|0|0|1|1|1|
'can_comment' => 2, // |0|0|0|0|0|0|1|0|
'can_edit' => 4, // |0|0|0|0|0|1|0|0|
'can_delete' => 8, // |0|0|0|0|1|0|0|0|
);
//var_dump($user_perms);
if ($user_perms & $perms['can_post']) {
echo 'has permission to post <br />';
}
if ($user_perms & $perms['can_comment']) {
echo 'has permission to comment <br />';
}
if ($user_perms & $perms['can_edit']) {
echo 'has permission to edit <br />';
}
if ($user_perms & $perms['can_delete']) {
echo 'has permission to delete <br />';
}
// or
if ($user_perms | $perms['can_delete']) {
echo 'He/She can delete <br />';
}
// xor exclusive
if ($user_perms ^ $perms['can_delete']) {
echo 'He/She can delete <br />';
}
// I need to check about precedence (What is the order of the comparation
if ($user_perms & 1 | 2 & 4) {
echo 'ouuu <br />';
}
// not or unary operator
if ($user_perms & ~7) {
echo 'unary <br />';
}
// not or unary operator
if ((5 & 3) & 1) {
echo 'unary 2 <br />';
}
echo 5 << 1; // each step means "multiply by two" ( = 10 )
echo '<br />';
echo 8 >> 1; // each step means "divided by two" ( = 4 )
echo '<br />';
echo 5 << 2; // each step means "multiply by two" ( = 20 )
echo '<br />';
echo 7 ^ 2; // it results 5
echo '<br />';
echo 7 & 2; // it results 2
echo '<br />';
echo (7 | 2); // it results 7
echo '<br />';
echo (7 | 2) >> 1; // it results 3
error_log("Big trouble, we're all out of FOOs!", 42,
0);