I use bitwise operations to represent many access control flags within a single whole.
ADMIN_ACCESS = 1; EDIT_ACCOUNT_ACCESS = 2; EDIT_ORDER_ACCESS = 4; var myAccess = 3; // ie: ( ADMIN_ACCESS | EDIT_ACCOUNT_ACCESS ) if ( myAccess & EDIT_ACCOUNT_ACCESS ) { // check for correct access // allow for editing of account }
Most of this happens on the PHP side of my project. However, there is one part where Javascript is used to combine multiple access flags using | while maintaining the access level. It works great. I found that after an integer (flag) gets too large (> 32 bits), it no longer works correctly with bitwise operators in Javascript. For instance:
alert( 4294967296 | 1 );
I am trying to find a workaround for this, so I donβt need to limit the number of access control flags to 32. Each access control flag is twice the previous control flag, so that each control flag will not interfere with the other control flags.
dec(4) = bin(100) dec(8) = bin(1000) dec(16) = bin(10000)
I noticed that adding two of these flags along with simple + , it seems to come out with the same answer as the bitwise or operation, but I am having problems wrapping my head around that, a simple substitution, or there may be problems with by this. Can anyone comment on the validity of this workaround? Example:
(4294967296 | 262144 | 524288) == (4294967296 + 262144 + 524288)
source share