28

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 have been doing PHP for several years now, and I have seen bitwise operations very rarely in PHP projects.

Can you give me examples of usage?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114

2 Answers2

46

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

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    how well this scale if you are to deal with an hypothetical big / variable number of preferences? – Elzo Valugi Jan 25 '10 at 12:08
  • 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 Jan 25 '10 at 12:34
  • 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 Jan 25 '10 at 12:40
  • 1
    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 Jan 25 '10 at 13:13
  • 3
    @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 Jan 25 '10 at 14:01
  • This is one of the best example I have seen for understanding this, thank you. I am trying to use bits http://stackoverflow.com/questions/5380506/improve-this-php-bitfield-class-for-settings-permissions – JasonDavis Mar 21 '11 at 19:06
  • @Elzo you can use my [infinite bitmask](https://github.com/Aliance/InfiniteBitmask) implementation. – Aliance Nov 21 '16 at 07:11
18

Bitwise operations are extremely useful in credentials information. For example:

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

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

and so on...

This way, we can keep a simple integer in one database column to have all credentials in the system.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TomaszSobczak
  • 2,900
  • 21
  • 24