Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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. -
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. -
@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 -
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
}
``` -
acelan485yDefinitely 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. -
SomeNone7135yIf multiple flags can be valid at one, using binary is the logical choice. If they are mutually exclusive, binary flags make little sense.
-
retnikt68945yYes, this is not only best practice, but also the only way to do it at all if you want to combine them
-
skydhash1345yUsing 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 -
skydhash1345yUPDATE
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 -
@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++.
-
@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 =)
Related Rants
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.
question
decimal
sql
php
bitwise