Although I grasp the concept of Bitwise Operators, I can\'t say that I have come across many use cases during the webdevelopment process at which I had to resort to using Bi
I use em every so often but never in places where I can avoid them. The most often I have used them are the following two situations.
My main use for bitwise operators could be relevant anywhere - representing a set of flags. For instance, you might have an integer in the database representing a set of security permissions for a user, and in your web app you would have to check those before continuing.
Those tend to only require &
and |
- e.g.
if ((permissions & Permission.CreateUser) != 0)
{
...
}
or
Permission requiredPermission = Permission.CreateUser
| Permission.ChangePassword;
Bit shifting operators are less useful in "business" applications in my experience.
I think bitwise operators are very strong if used intelligently.
Suppose you have a "Online Store". And some of your items fall in more that one Categories.
Either you have to Create a Many-to-Many relation. Or you can give your Categories an extra Binary-ID and in your product just store the Bitwise combinition of Categories-IDs
I think in few line I can't explain in detail. SORRY
For Java programmers the xor bitwise operator (^) provides a useful way to code an exclusive OR test between two booleans. Example:
boolean isFoo = ...
boolean isBar = ...
if (isFoo ^ isBar) {
// Either isFoo is true or isBar is true, but not both.
Note: there's no actual bit manipulation going on here but it is a useful way to use the xor bitwise operator (in the web tier or anywhere else).
(Same may apply to C#, since it's so similar to Java. Not sure, though.)
On the off-topic side of things, in high-level languages, especially in parsed languages (such as PHP), bitwise operations are tons slower[citation needed] than the normal arithmetic. So, while Jon's permission checking might be ok from a performance standpoint, it's not very 'native' in the web domain.
Generally, you don't need to concern about operations at the bit level. You can think in bytes, ints, doubles, and other higher level data types. But there are times when you'd like to be able to go to the level of an individual bit.
One of the most common utilization cases of the bitwise operators are the flags (php example). The bitwise operators are also used in binary file IO operations.