Bitwise works on the binary level, so 0 on binary would seen as 0000_0000, and (in two's complemented) -1 is 1111_1111, this not 0 flips all the bits to 1s, thus alters 0 into -1.
117 Bitwise operations, including bit shift, are fundamental to low-level hardware or embedded programming. If you read a specification for a device or even some binary file formats, you will see bytes, words, and dwords, broken up into non-byte aligned bitfields, which contain various values of interest.
These are the bitwise operators, all supported in JavaScript: op1 & op2 -- The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.
1 Using bitwise_and, bitwise_or, and bitwise_not you can modify any bit configurations to another bit configurations (i.e. these set of operators are "functionally complete"). However, for operations like modulus, the general formula would be necessarily be quite complicated, I wouldn't even bother trying to recreate it.
I tried to understand how if condition work with bitwise operators. A way to check if a number is even or odd can be done by: #include <iostream> #include <string> using namespace std;
Related: Bitwise operation and usage for bitwise boolean ops in general, pointing out that they do 32 (or 64 or whatever) separate bitwise boolean operations in parallel.
C operators are meant to map pretty much directly to assembly, and there is very much a difference between, e.g., a bitwise AND and modulo or division. The only reason they tend to have the same performance is that the compiler is smart enough to ignore the actual code and optimise it into the bitwise operation before the scenes.