bit-manipulation

When to use Bitwise Operators during webdevelopment?


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 Bitwise Operators.

Please remember that this question is specifically intended for use of Bitwise Operators in web languages.


Solution

  • 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.