Game Development Community

Combining Binary Numbers

by James "Nycto" Frasca · in Technical Issues · 06/20/2004 (5:57 pm) · 4 replies

This isn't really game related except that the CMS I am making could be used for game websites. I thought someone in this community might have an answer for my question, though.

I'm working on a simple PHP permissions function at the moment. Users belong to groups and those groups have number associated with them that represent a permission level. Translated in to binary, the number is a bunch of yes/nos. Its a common setup, but I'm trying to be clear.

I want to find out if the current user has access to something with permission level of 8 (1000 in binary). He belongs to a group that has a permission level of 27 (11011). Thus, he has permission to view that area of the site.

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 a function that spits out 27 (11011).

more examples:
5 (101) + 1 (001) = 5 (101)
6 (110) + 5 (101) = 7 (111)
3 (011) + 2 (010) = 3 (011)

I have a way to do it right now, but it involves moving through each position in the binary strings and comparing them. There must be a better way to do this; some algorithm. I have tried searching the web, but I am not totally sure what I am searching for.

Thanks for your help.

#1
06/20/2004 (5:59 pm)
Just use a bitwise or operation on the two groups.
#2
06/20/2004 (6:08 pm)
Yup. The standard bitwise operators are:

|   or
&   and
^   xor
~   not

Handy critters they are.
#3
06/20/2004 (6:20 pm)
Thanks for pointing me in the right direction, guys. Worked like a charm
#4
06/20/2004 (7:21 pm)
Glad to help. :)