Bitwise Operators – Revisited
A long while ago Chris had made a blog post on “Bitwise Operators“. Now, just to make it crystal clear, Bitwise Operators are not in MyBB 1.4. MyBB 1.2 was more focused on the code. MyBB 1.4 is focused more on features. Sorry folks, but there isn’t just enough time in the world!
However, I’d like to bring them up again and revisit the idea of what they are and how they work.
To re-iterate what Chris stated on his blog post, Bitwise operators are operators that work on a bit-level. You can assign “levels” to a number. This number indicates the level and later on, will be able to provide us with what users have what permissions. In turn you have an array of users each assigned a specific value.
We’ll use Chris’s crisp and clean example to show how bitwise operators work:
<?php
$notices['can_view'] = 1;
$notices['can_post_threads'] = 2;
$notices['can_post_replies'] = 4;
$notices['can_edit'] = 8;$testers[1] = 1;
$testers[2] = 5;
$testers[3] = 9;
$testers[4] = 14;foreach($testers as $key => $tester)
{
echo “$key”;
foreach($notices as $key => $notice)
{
if($tester & $notice)
{
echo ” – $key”;
}
}
echo “<br />”;
}
?>
Now this code will print out the following:
1 - can_view
2 - can_view - can_post_replies
3 - can_view - can_edit
4 - can_post_threads - can_post_replies - can_edit
Now you may be asking how we got that? In case you missed it, tester 4 has a value of 14. Now look at what the tester got: “can_post_threads - can_post_replies - can_edit". can_posts_threads has a value of 2, can_post_replies has a value of 4, and can_edit has a value of 8. Now what does that all add up to? 14! And so on and so forth; See? Simple!
This system can make our most difficult permissions night-mares as easy as the above example. If our course doesn’t change, I believe you will be seeing a lot more of Mr. Bitwise in 2.0 than ever before.
Jeff Chan said,
Isn’t this kind of like the unix chmod permission system? Very interesting. It’s actually very useful for so many things.
There will be no Bitwise Operators in 1.4 | MyBB Games '07 said,
[...] Whether this is the stale part of the cake, who knows. I sure hope it isn’t. You can read more about Bitwise Operators in terms of MyBB at Chris’ blog, and at Ryan’s blog. [...]
Tikitiki said,
Yes indeed Jeff. The CHMOD Permissions system was based off of bitwise operators.
Add A Comment