(PHP) Working With Bitmasks

No, not secret CIA crap.. but just about as tough to crack for most of us. He's got it all, css, html, xhtml, php.. you name it!

Moderator: Zorro

(PHP) Working With Bitmasks

Postby Zorro » March 16th, 2011, 5:10 pm

Scenerio

* You have a complex and evolving application.
* You have database entries that have an ever growing list of boolean attributes.
* The database is becomming too large and complex for so many true/false (boolean) field columns.

Example

* The company is in need of translators to handle the language translations that their product supports.
* You have a database that stores job applications.
* You need a way to store the selection of foreign languages the applicant is fluent in (i.e. as checkboxes).
* You need a way to easily change/edit the language selection for each applicant.
* You need a way to easily add more language options as the company's support for languages expands.

Solution 1 - Boolean Columns

Have a database column in the 'job_applicants' table with a Y/N or 1/0 enumerated type (ENUM) for every language the company supports.

The Problem With This Solution

* The database can quickly become cluttered with so many boolean columns.
* The database would require constant structure changes each time the company adds support for another language.
* The database could become large and inefficient.

Solution 2 - Bitmasks

The idea of Bitmasks may seem complex at first, but once you understand the logic, it's a breeze. Let's just cut straight to the point.


Code: Select all
<?php

$languages = array(
   'English'  => 1,
   'Spanish'  => 2,
   'German'   => 4,
   'Martian'  => 8,
   'Russian'  => 16,
   'Arabic'   => 32,
   'French'   => 64,
   'Chinese'  => 128,
   'Japanese' => 256,
);

$myLanguages = 85;

foreach ($languages as $language => $bitmask) {
   if ($myLanguages & $bitmask) echo 'Yes - ' . $language . '<br />';
   else echo 'No - ' . $language . '<br />';
}

?>



For those of you who are unable to actually test the code above, the code is simple enough to easily convert into your language of choice.

So what's going on? Well, if you actually run the code above, you'll get a result like this:


Code: Select all
Yes - English
No  - Spanish
Yes - German
No  - Martian
Yes - Russian
No  - Arabic
Yes - French
No  - Chinese
No  - Japanese



So how does it work? Well, rather than me explaining it, figure it out yourself.

1. Look at the yes/no result above.
2. Each language represents a number. Start your total at 0.
3. For each language that says 'No', do nothing.
4. For each language that says 'Yes', add the number representing that language to the total.
5. Here's what you'll end up with.


Code: Select all
+ 1 English
+ 0 Spanish
+ 4 German
+ 0 Martian
+ 16 Russion
+ 0 Arabic
+ 64 French
+ 0 Chinese
+ 0 Japanese
= 85 TOTAL (the value of $myLanguages)



And there you have it. A single number stored in a database column can say a lot. Go ahead and drop those boolean columns, already.

Over and out.
Research shows that one in three Clinton supporters are just as stupid as the other two.
Zorro
Squad Member
 
Posts: 543
Joined: February 5th, 2011, 12:25 pm

Re: (PHP) Working With Bitmasks

Postby Bullet Magnet » March 22nd, 2011, 5:56 pm

I like writing programming tutorials too.

Do I get a C# section?
User avatar
Bullet Magnet
Squad Member
 
Posts: 1472
Joined: February 6th, 2011, 5:53 pm

Re: (PHP) Working With Bitmasks

Postby BlackCat » March 22nd, 2011, 9:47 pm

Someone was willing to pay money to get help with HTML on the gigs section of the CL. I gave him a lesson for free and he was thankful. With the little HTML I know, I was shocked that I could actually help someone.
User avatar
BlackCat
 
Posts: 1674
Joined: February 12th, 2011, 12:05 pm


Return to El Zorro's casa de la Codes

Who is online

Users browsing this forum: No registered users and 23 guests

cron