Answer the question
In order to leave comments, you need to log in
What are bitwise operations for?
Good afternoon.
I no longer consider myself a beginner in programming, but I still don’t understand what shifts to the right, left by bits are for. I imagined what they are for in c ++ (low-level programming). but when I met these operations in javascript I fell into a stupor. In the books that I read, I did not find clear explanations. I understand the purpose of bitwise and, or, not, exclusive or, but I don’t know where shifts are applied. I will be glad to any answer.
Answer the question
In order to leave comments, you need to log in
They are needed for:
You see, this is not a feature of the language - it is rather a feature of our processors, principles of computation, assembler. Since these operations exist, and in the form in which they are used to, it is logical to assume that many programs and algorithms rely on their presence in the developer's toolkit. Imagine that you would have to port some cryptographic algorithm using these operators to js in which they would not exist. Let's say you implement it by multiplying / dividing by two, but then you find that when you multiply, the most significant bit disappears, but should become the least significant. In addition, your js implementation would be much slower than the language's built-in implementation.
Why do bit shifts even exist? But because they are performed by a very simple circuit - a shift register. And instead of carrying out multiplication (at first it was dozens of cycles), we connect this very register to the processor registers, and we will do everything in one cycle. There was even a program that generated a quick multiplication code for a constant, for example, 6 - shift, add and shift again. And at one time it was faster than mul - and now we write mul eax, without a twinge of conscience, 6.
What can shifts do, but is implemented with a creak through everything else? Access to the i-th bit.
i-th bit = (x >> i) & 1
According to the rules of good programmer tone, multiplication, even by 2 or 4, is written as a regular * (>> is written if the compiler is impenetrably stupid and the multiplication is slower). But shifts - it is them, and not multiplication - are written when working with bit fields.
There are also special shifts.
* Arithmetic right shift (left is the same as normal) - for signed numbers.
* Shift-Rotate - mainly for cryptography.
* Shift-rotate through register flags - mainly for long arithmetic.
Bitwise operations are needed to change individual bits in values. For the most part, if you do not take into account fast algorithms, then most often bitwise operations are needed to work with masks. Often, with the help of these, some flags are set, or, as Man pointed out , the same subnet mask, knowing which and knowing the IP address of the current machine, you can find out the IP address of the host and the entire range of IP addresses of the network due to the banal operation of the bitwise and. There are also tasks related to encoding information, there is bit / byte stuffing, data packaging (hello shifts), etc.
In a word, it would be hard without bitwise operations, and then it would be impossible, say, using FileApi to open some kind of binary data format on the client.
At least for multiplication / division of a number suitable for the formula 2 ^ n. In any language, this will be a performance gain (with the exception of a very smart compiler).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question