Log in

View Full Version : How to decode user options


hardwareirc.com
06-05-2005, 06:21 AM
Hello,
I would like to use my forum for mailing list purposes too, and I want to send emails only to the members that have the "Receive emails" option enabled.

I looked on the vbulletin 3 database, and I saw that the user options preferences are coded with a number, so it's quite difficult to see which number represents the desire to receive emails...

Can anyone help me "decoding" the number?

Thanks ;)

Andreas
06-05-2005, 10:18 AM
The user options are just a bitfield.
From init.php

// Defined constants used for user field.
$_USEROPTIONS = array(
'showsignatures' => 1,
'showavatars' => 2,
'showimages' => 4,
'coppauser' => 8,
'adminemail' => 16,



So if you want to get all users which have this options enabled;:

SELECT username, email FROM user WHERE options & 16

hardwareirc.com
06-05-2005, 05:04 PM
The user options are just a bitfield.
From init.php

// Defined constants used for user field.
$_USEROPTIONS = array(
'showsignatures' => 1,
'showavatars' => 2,
'showimages' => 4,
'coppauser' => 8,
'adminemail' => 16,



So if you want to get all users which have this options enabled;:

SELECT username, email FROM user WHERE options & 16
Great ;)
It works fine :D

hardwareirc.com
06-08-2005, 11:01 AM
Hi guys, I'm not familiar with bitfields... There's a way to set the bit #16 to 0 with just a mysql query?

Thanks ;)

Andreas
06-08-2005, 12:16 PM
Yes. Bit #16 = 2^15 = 32768
UPDATE table SET field=field-32768 WHERE field & 32768

Or to set it where it is not set
UPDATE table SET field=field+32768 WHERE NOT (field & 32768)

Paul M
06-08-2005, 12:22 PM
Except, I think he means the bit valued at 16, not the 16th bit . In the above options case the value should be 16. :)

Andreas
06-08-2005, 12:26 PM
But he said bit #16 ;)

Paul M
06-08-2005, 12:28 PM
But he said bit #16 ;)Technically Yes, because he doesn't understand bit fields - read the posts and I think you will agree with me. :)

hardwareirc.com
06-08-2005, 12:48 PM
Technically Yes, because he doesn't understand bit fields - read the posts and I think you will agree with me. :)
hmmm... well, could you explain me bitfields in order to avoid any further doubts? :P

or where could I find more info? google doesn't help :(

Paul M
06-08-2005, 12:54 PM
Correct me if I'm wrong - but you want to set the 'adminemail' option (which is bit 5, value 16) to zero ?

Andreas
06-08-2005, 01:39 PM
OK, let's start at 0 :)
Computers can only handle two signal states: Low and High (Voltage).
This represents a Bit, eg. 0 or 1.
All Numbers are constructed as a series of bits, for example 01101001.
This is called the binary or dual system, in opposite to the decimal system we use for everyday calculations.
As there are only 2 Values (0 and 1), the base for this is 2.
How do we determine the value of a number in the decimal system:
For eamle 126 is One Hundred and Twenytsix, or One Hundred + Tweny + Six, or formally:
6*10^0 + 2*10^2 + 1*10^2
This works the same way in dual system, but with 2 as base:
10000 = 0*2^0 + 0*2^1 + 0*2^2 + 0*2^3 + 0*2^4 = 16

As you can see, 16 represents the 5th bit.

Now we got some decimal number, let's say 287654.

How do we know if the 5th bit is set?
First we convert this to a number in dual system by cintinued division:

287654 / 2 = 143827 + 0
143827 / 2 = 71913 + 1
71913 / 2 = 35956 + 1
35956 / 2 = 17978 + 0
17978 / 2 = 8989 + 0
8989 / 2 = 4494 + 1
4494 / 2 = 2247 + 0
2247 / 2 = 1123 + 1
1123 / 2 = 561 + 1
561 / 2 = 280 + 1
280 / 2 = 140 + 0
140 / 2 = 70 + 0
70 / 2 = 35 + 0
35 / 2 = 17 + 1
17 / 2 = 8 + 1
8 / 2 = 4 + 0
4 / 2 = 2 + 0
2 / 2 = 1 + 0
1 / 2 = 0 + 1

Now we write down the remaining values in reversed order:

1000110001110100110

This is decimal 287654 in dual system.

Now we can check if bit #5 (decimal 16, dual 10000) is set:

1000110001110100110
10000


Technically, this is a AND operation. Every bit position is compared - if both are the same the result-bit at this position is 1:

1000110001110100110
& 10000
= 0000000000000000000

As the can see, the result is 0.

But if we had for example

1000110001110110110
& 10000
= 0000000000000010000

Here the result is not 0, eg. the bit is set.
What is the difference between 1000110001110100110 and 1000110001110110110?

We already know that 1000110001110100110 is 287654 because we started with this.
So let's convert 1000110001110110110 to decimal:

0*2^0 + 1*2^1 + 1*2^1 + 0*2^2 ... = 287670.

As you can see, the difference is 16, as expected as this represents the bit we just set.

Maybe this makes things a bit clearer - or got you totally confused :D

hardwareirc.com
06-08-2005, 03:34 PM
I know perfectly how numbers are represented and the decimal and binary coding systems.

This is the problem:

after the first replies to this thread I understood that the binary string with all the on-off (0-1) settings was stored as an integer in mysql.

// Defined constants used for user field.
$_USEROPTIONS = array(
'showsignatures' => 1,
'showavatars' => 2,
'showimages' => 4,
'coppauser' => 8,
'adminemail' => 16,


From this code I thought that if I want to see if the signature is enabled i have to check the bit #1, #2 for avatar, #4 for images, #8 for coppa, and #16 for email.

I suppose that the "WHERE options & 16" condition goes to check the bit #16 if is "ON" or "TRUE", or... 1!

Am I wrong?

The other posts (16 or not 16 :P ) confused me a bit...

Andreas
06-08-2005, 04:10 PM
$_USEROPTIONS = array(
'showsignatures' => 1,
'showavatars' => 2,
'showimages' => 4,
'coppauser' => 8,
'adminemail' => 16,
'showvcard' => 32,
'dstauto' => 64,
'dstonoff' => 128,
'showemail' => 256,
'invisible' => 512,
'showreputation' => 1024,
'receivepm' => 2048,
'emailonpm' => 4096,
'hasaccessmask' => 8192,
//'emailnotification' => 16384,
'postorder' => 32768,
);

From this code I thought that if I want to see if the signature is enabled i have to check the bit #1, #2 for avatar, #4 for images, #8 for coppa, and #16 for email.


Check Bit #4096 to see if the user want's to receive notifications about new PMs via eMail?
Maybe dozenzs of years in the future ... 65535 Bit-Systems ^.^

No. These are decimal values.

WHERE options & 16 checks if Bit #5 (2^4=16) is set.

hardwareirc.com
06-08-2005, 04:46 PM
Check Bit #4096 to see if the user want's to receive notifications about new PMs via eMail?
Maybe dozenzs of years in the future ... 65535 Bit-Systems ^.^

No. These are decimal values.

WHERE options & 16 checks if Bit #5 (2^4=16) is set.
Oh, well... now it's clear ;)

I wasn't able to figure out the big numbers of the options field ;)

Perhaps do you know where can I find some documentation about the & operator?

Thanks! ;)

Oh, well... now it's clear ;)

I wasn't able to figure out the big numbers of the options field ;)

Perhaps do you know where can I find some documentation about the & operator?

Thanks! ;)
Oh... sorry, I d0n't know where do I have my mind today.... I asked for the documentation of the bitwise AND operator :P :nervous: :ninja: :D :D