tags:

views:

312

answers:

3

I understand that bitwise operations are necessary for much low-level programming, such as writing device drivers, low-level graphics, communications protocol packet assembly and decoding. I am doing PHP for several years now, I have seen bitwise operations very rarely in PHP projects.

Can you give me examples of usage?

+3  A: 

Very little in PHP uses bitwise operations outside of optimization tricks as well as configuration such as error_reporting().

Ignacio Vazquez-Abrams
True, except for the "very little" part. It's used very often, especially in function parameters. For instance, glob($pattern, GLOB_ONLYDIR | GLOB_MARK), as well as error_reporting() etc.
GZipp
+2  A: 

bitwise operation are extremely usefull in credentials info for example

function is_moderator($credentials)
{ return $credentials & 4; }

function is_admin($credentials)
{ return $credentials & 8; }

and so on... this way we can keep simple integer in 1 database column to have all credentials in system

tomaszsobczak
+6  A: 

You could use it for bitmasks to encode combinations of things. Basically, it works by giving each bit a meaning, so if you have 00000000, each bit represents something, in addition to being a single decimal number as well. Let's say I have some preferences for users I want to store, but my database is very limited in terms of storage. I could simply store the decimal number and derive from this, which preferences are selected, e.g. 9 is 2^3 + 2^0 is 00001001, so the user has preference 1 and preference 4.

 00000000 Meaning       Bin Dec    | Examples
 │││││││└ Preference 1  2^0   1    | Pref 1+2   is Dec   3 is 00000011
 ││││││└─ Preference 2  2^1   2    | Pref 1+8   is Dec 129 is 10000001
 │││││└── Preference 3  2^2   4    | Pref 3,4+6 is Dec  44 is 00101100
 ││││└─── Preference 4  2^3   8    | all Prefs  is Dec 255 is 11111111
 │││└──── Preference 5  2^4  16    |
 ││└───── Preference 6  2^5  32    | etc ...
 │└────── Preference 7  2^6  64    |
 └─────── Preference 8  2^7 128    |

Further reading

Gordon
how well this scale if you are to deal with an hypothetical big / variable number of preferences?
Elzo Valugi
Do you mean speedwise or the number of possible preferences? The maximum number on a 32bit OS would be 31 preferences for a single bitmask. You could combine multiple bitmasks via arrays though. See the comments on http://php.net/manual/en/language.operators.bitwise.php
Gordon
Yes, a limit of 31 preferences is not scalable compared with an array, isn't it? I guess you can combine bitmasks but will this code be easy to debug compared with an array? it will that much faster?
Elzo Valugi
Well, slap a nice API around it and it might be easy to debug. I could imagine bitmasks to be faster than arrays, but I've never benchmarked them. I rarely use bitmasks. You just asked for an example for bitwise operations and bitmasks are one :)
Gordon
thanks. you are right
Elzo Valugi
@Elzo Your absolutely right, it's harder to debug/maintain and can actually be slower than using distinct values set to true or false. It's almost always better to use explicitly named boolean values for exposed via an API and when storing those values in database. It's really only good in a small number of cases. Such as, if you have a specific known requirement that you need to be extremely efficient in terms of storage footprint (e.g. embedded systems), or (a more recent paradigm) you want to encode a number of options as parameters in a URL, while keeping it short.
Iain Collins