+ 15
What is the use of bitwise operators in real life?
9 Answers
+ 12
We can better pack data fields within bytes efficienciently to reduce the amount of memory or disk space used by servers and access the values within these fields using bitwise operators and masks.
+ 8
It is a fast and simple action, directly supported by the processor, and is used to manipulate values for comparisons and calculations.
So, this is used mainly on your PC.
If you are reffering to humans, we dont use this. :))
+ 8
A chessboard has 64 squares and an integer typically has 64 bits.
So in chess engine programming, so-called "bitboards" are very common, and you can encode for example "all squares that contain a white pawn" as just a single number.
Which is both fast and needs very little space.
To get and set individual squares of the bitboard you will need bitwise operators like |, &, >>, <<.
+ 5
In a single byte (8 bits) we can only store numbers from 0-255. Google came up with a nice way to store larger numbers (in protocol buffers):
We use only 7 bits of the number, and the 8th bit will be used to check if we need to read more bytes. If it is 0, the number is done, if it is 1, the number continues in the next byte.
To check if the "we are done"-bit is set, we simply do a binary `if(number & 128)`. To get the other 7 bits we do `number & 127`.
That means we can have 7-, 14-, 21-, ... bit numbers and store any number we want with minimal space usage.
+ 5
I have also question, do we use bitwise whan we build common web pages? I am still begginer do i need to force myself and learn all that? Or is better to use reference if i ever need it.
+ 5
Sanja Panic You almost never see them in web development so no need to memorize them.
Though I do recommend everyone (programmer or not) to learn binary and how to convert between decimal/binary/hex. It's what computers are built on (and it's easy to learn).
+ 3
When programming microcontrollers they are very
Useful,(since for telling whether a device is on or off a single bit is sufficient) bitwise operators help in masking certain biys whenever required
+ 2
It depends on what do you mean by real life? You can modifie data of a stream, optimize algorithm or other things
+ 1
Some of the common use cases of bitwise in JavaScript, check out this code
https://code.sololearn.com/cvOWut2HJNHW/?ref=app