# How to find flag QA_USER_FLAGS_USER_BLOCKED from user flag?

+1 vote
1.5k views
in Q2A Core
edited

I was able to get blocked users from the database. To make it more efficient I need to understand the flag system better. Once Gidgreen explained it a bit to me.

"you can see the QA_USER_FLAGS_* constants at the top of qa-app-users.php for the bit values." gidgreen

These are the flags:

define('QA_USER_FLAGS_EMAIL_CONFIRMED', 1);
define('QA_USER_FLAGS_USER_BLOCKED', 2);
define('QA_USER_FLAGS_SHOW_AVATAR', 4);
define('QA_USER_FLAGS_SHOW_GRAVATAR', 8);
define('QA_USER_FLAGS_NO_MESSAGES', 16);
define('QA_USER_FLAGS_NO_MAILINGS', 32);
define('QA_USER_FLAGS_WELCOME_NOTICE', 64);
define('QA_USER_FLAGS_MUST_CONFIRM', 128);

I try to find the blocked users by the flag which would be "2". How to see if a "2" is contained in the flag number. E.g. "3" is email confirmed (1) + user blocked (2)?

by
edited by

okay, I needed some more brain work see the following as a tutorial for newbies

Decimal Binary
1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000

So I get a number such as 5 and convert it to binary: 0101

Thus I see the flags 1 and 4!

next step... OR statement. To transfer the value. I want to add flag 8 to 5.

example: 8 OR 5 = 8|5 is 1000 | 0101 results in

1 | 0 → 1
0 | 1 → 1
0 | 0 → 0
0 | 1 → 1

and get 1101 → 2^3+2^2+2^1+2^0 = 13

getting it slowly :)

next: how to check if value is inside we use AND

example: is flag 4 inside 13?

13: 1101
4: 0100

using the logical AND statement

1 & 0 → 0
1 & 1 → 1
0 & 0 → 0
1 & 0 → 0

we get 0100 which is 2^2 = 4.

so in PHP: (4 & 13) returns 4, then flag is inside!

if you try (4 & 16) it returns 0, flag not inside.

after writing this I found the php: bitwise operators reference.

by
I think you overcomplicated things there. If the user's flag number is stored in a variable \$userflags, then you can simply do:

QA_USER_FLAGS_USER_BLOCKED & \$userflags

Note the single &. If that statement comes out to true (or rather, 1) then the flag is set. In other words:

if ( QA_USER_FLAGS_USER_BLOCKED & \$userflags ) {
// the user is blocked
}
by