When to use Bitwise Operators during webdevelopment?

后端 未结 10 1307
Happy的楠姐
Happy的楠姐 2020-12-12 12:01

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

10条回答
  •  萌比男神i
    2020-12-12 12:12

    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.

提交回复
热议问题