Announcement

Collapse
No announcement yet.

bitfields

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • merk
    replied
    vBulletin 3 uses alot of constants

    Leave a comment:


  • Son-Goku
    replied
    Originally posted by i am coo man
    What is a bitfeild? What is the syntax to use em? Can you show code example? Or link!

    I dunnoz ![img]images/smilies/eek.gif[/img]
    2 examples are better than 1.
    PHP Code:
    <?php
     define
    ('BIT_1'1); // 0001
     
    define('BIT_2'2); // 0010
     
    define('BIT_3'4); // 0100
     
    define('BIT_4'8); // 0000 1000
     
    define('BIT_5'16); // 0001 0000 - see how all of this numbers have just one 1, so they can later on only change 1 bit
     // etc.
     
    $bitfield 0;
     echo 
    $bitfield// 0 (0000)
     
    $bitfield ^= BIT_1;
     echo 
    $bitfield// 1 (0001)
     
    if($bitfield BIT_1) echo "Both include a 1 at the same position, so the returned value is 1 and our if statement true";
     
    $bitfield ^= BIT_5;
     echo 
    $bitfield// (0001 0001)
     
    if($bitfield BIT_1) echo "Still returns a 1 at the same position";
     if(
    $bitfield BIT_5) echo "But it also returns a 1 at this position now";
     
    $bitfield ^= BIT_1;
     echo 
    $bitfield// 16 (0001 0000) - we used toggle (^=) to unset BIT_1
     
    if($bitfield  BIT_1) echo "This message should never appear, because & checks for bits set in both operands, it should return 0";
     if(
    $bitfield BIT_5) echo "BIT_5 is still set.";
     
    ?>
    it's obvious that this saves a lot of memory / space, since a standard integer offers 32 "booleans" using only 4 byte that way. (smallint 2 byte = 16 bit).

    NOTE: you should use UNSIGNED because otherwise the first bit "decides" about it being a positive (0) or negative (1) number, so you'd have to use negative numbers which are harder to find out for the first bit (normally it's always last-value*2: 2, 4, 8 etc.).

    i didn't test the code so i can't assure that it'll work as is, but you get the idea of how it works.


    just out of interest (and kind of off-topic but i didn't feel it was worth a new thread), i've always wondered how constants in php work memory wise. afaik in c the preprocessor replaces every constant with it's value, so the constant does not really exist (and thus doesn't use memory) right? if it's the same in php, wouldn't it be better to use a lot of constants instead of variables which you don't change anyway?
    maybe not that important, but still interesting to know.

    Leave a comment:


  • merk
    replied
    A bitfield is a field that contains only 1 number

    They are certain numbers.

    1, 2, 4, 8, 16, 32, so on.

    If you look in init.php, youll find alot of different permissions defined,

    PHP Code:
    $_BITFIELD['usergroup']['adminpermissions'] = array(
    'ismoderator'         => 1,
    'cancontrolpanel'     => 2,
    'canadminsettings'     => 4,
    'canadminstyles'        => 8,
    'canadminlanguages'     => 16,
    'canadminforums'        => 32,
    'canadminthreads'     => 64,
    'canadmincalendars'     => 128,
    'canadminusers'         => 256,
    'canadminpermissions' => 512,
    ......... 
    (the vB code converts each permission into a static variable, so they would become ISMODERATOR, CANCONTROLPANEL, etc..)

    Then, you could check using a sql query, if the user had permission,
    Code:
    SELECT * FROM usergroup WHERE (adminpermissions & CANCONTROLPANEL)
    Or, in php
    PHP Code:
    if($usergroup['adminpermissions'] [b]&[/bCANCONTROLPANEL) { //user can control panel } 
    Hope i explained it

    You will find information about "bitwise" operations at http://au.php.net/manual/en/language...rs.bitwise.php and
    http://www.mysql.com/doc/en/Bit_functions.html
    Last edited by merk; Sun 31 Aug '03, 11:57pm.

    Leave a comment:


  • i am coo man
    replied
    What is a bitfeild? What is the syntax to use em? Can you show code example? Or link!

    I dunnoz

    Leave a comment:


  • Scott MacVicar
    replied
    its used in vB3, though i believe a few dbms dont support bitwise operations in queries which is a drag.

    Leave a comment:


  • Son-Goku
    started a topic bitfields

    bitfields

    can anyone enlighten me why nobody seems to use bitfields in php? i've learned how great they are when working in c, an unsigned smallint can be used to hold 16 "booleans" that way.

    i didn't look at vb3 (so i don't know if it was used there), but in vb2 it could be used in the usergroup table for example. there are 28 yes / no options, which use 28 columns when it could've been just 1 (!) int field.

Related Topics

Collapse

Working...
X