3

Need opinions: When your knowledgeable colleague backend-developer chooses 1,2,4,8,16 as enum values instead of 1,2,3,4,5 (for roles associated with permissions, which may be cumulatable) in order to be able to do bitwise operations, is it a sound decision for this scenario? Is it a best practice, just as good, or pedantic?

I want to master bitwise but have a hard time grasping such operations as quickly as logical ones.

Comments
  • 6
    Programming without understanding bit operations is a foreign concept to me. I would spend some time learning how to do bit operations in your chosen language.

    In C/C++ you see bit operations a lot. I have even done bit operations in Python.

    If you want to make it fun then get an Arduino and do bit operations in there. You can get down to the nuts and volts there.
  • 3
    If you have a bunch of largely independent binary options which can be mixed easily, bitwise ftw. If you have a more complex system then no.
    Unix file permissions is a great example for a case when bitwise works well.
  • 0
    @Demolishun I mainly do Javascript/PHP (in lesser amounts), and have never encountered bitwise operations in professional code in the 4 years I've been employed as developer.

    The only time I did was when diving into source code of low-level image manipulation and base64 encoding algorithms (but hand-coding these are things you almost never do in Javascript).

    @RememberMe thanks for the advice
  • 1
    It's useful if the statuses are related and often checked together.

    Eg. You have 4 flags, and you need to do something if the last 2 flags are ON.

    you can do
    ```
    if ((statuses & 0x03) == 0x03) {
    // do stuff
    }
    ```
  • 0
    Definitely using bitwise if wanting to have flexible roles/permissions. In company where I worked before, we used them well over ten years.

    Incremental user levels works for small scale. Once there are more options and need to have wide variety different permissions, linear userlevels cause huge mess, where logic doesn't help anymore.
  • 0
    If multiple flags can be valid at one, using binary is the logical choice. If they are mutually exclusive, binary flags make little sense.
  • 0
    Yes, this is not only best practice, but also the only way to do it at all if you want to combine them
  • 1
    Using byte-sized variables:
    1 -> 0000 0001
    2 -> 0000 0010
    4 -> 0000 0100
    8 -> 0000 1000
    and so on

    Those numbers have only one bit on. It made it really easy to do bitwise operations like ORing them to set multiple options
    4 | 8 = 12 -> 0000 1100
    And extracting them later
    12 & 4 = 4
    12 & 8 = 8
    12 & 12 = 12
    12 & anything else = 0

    Basically, you choose powers of 2 and OR them to combine them and AND them to extract them
  • 0
    UPDATE
    You can also use AND with the NOT version of the option to unset it like:
    12 & !4 = 8
    12 & !8 = 4
    12 & !12 = 0
    Anything & !12 = XXXX 00XX, where the X will stay the same
  • 0
    @webketje Ah, I see. I am a systems level guy so I use bitwise stuff all the time. I have used them in javascript that runs in QML. It was to select flags for window modes. I don't think I have used it in javasript or php for browsers though. If you decide to do system level code you will want to know about them. Or perhaps application development in C++.
  • 0
    @skydhash thanks for the summary, and to everyone else thanks for the feedback! I'm looking forward to doing some exercises with them soon and will post an update when I get there =)
Add Comment