|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
combining binary numbers
I'm working on a simple PHP permissions function at the moment. Users belong to any number of groups, and those groups have a binary number (saved in base 10) that represents whether they have access to one aspect of the site. It's a common method, but let me give an example just to clarify.
I want to find out if the current user has access to something with permission level of 8... 1000. He belongs to a group that has a permission level of 27... 11011. Thus, he has permission to view the content. Now, a user can belong to multiple groups. I am looking for an easy method (hopefully a formula) to combine the permission levels of two groups. Say a user belongs to a group with a perm level of 25 (11001) and one of 3 (11). I want 27 spit out (11011). I've been searching around, but I don't know exactly what to call this. Any help would be appreciated. 5 (101) + 1 (001) = 5 (101) 6 (110) + 5 (101) = 7 (111) 3 (011) + 2 (010) = 3 (011) Thanks a lot.
__________________
- Nycto |
|
#2
|
||||
|
||||
|
I found my answer in the form of Bitwise operators.
|
|
#3
|
||||
|
||||
|
For those who might be reading this, and don't know, the bitwise operator used is "or".
you would write: newPermission=old1perm|old2perm; (bitwise "or") 1000|0001=1001 1100|1001=1101
__________________
"Science is constructed of facts as a house is of stones. But a collection of facts is no more a science than a heap of stones is a house." - Henri Poincare |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > combining binary numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|