written by @supersebi
appinv told me to write notes about this, so ill do it i guess
Bitwise operations are operations that operate on the binary representations of numbers.
More specifically, bitwise operations operate on each bit in that binary representation seperately.
Supersebi3 – 08/24/2017
The bitwise operations are AND (&), OR (|), XOR (^) and NOT (~, ¬)
They work just like the logic gates with the same names, on each bit of the two numbers.
Lets say we want to do 214 & 185(edited)
The binary representations of these numbers are 11010110 and 10111001
Now we just do the and operation on each bit of the two numbers:
0 AND 1 = 0, 1 AND 0 = 0, 1 AND 0 = 0, 0 AND 1 = 0, 1 AND 1 = 1, 0 AND 1 = 0, 1 AND 0 = 0, 1 AND 1 = 1
Therefore, 214 & 185 = 0b10010000 = 144
The same technique is used for | (OR) and ^ (XOR)
The bitwise NOT (¬, represented as ~ in most programming languages) operateoperates on a single number, just like the logical NOT operates on a single bit:
Therefore, ¬ 214 = 0b00101001 = 41
A common application for the bitwise AND are flags
Flags are a kind of way to tell a program how it should behave.
In python, regex flags use the exact same algorithm I am going to tell you about
Since flags can either be on or off, they can effectively be stored as binary data.
Lets say our program has 8 available flags abcdefgh
Each flag gets a numeric value:
a = 128,
b = 64,
c = 32,
d = 16,
e = 8,
f = 4,
g = 2,
h = 1
To activate flags, they are added together
Lets say flags b, c, e and g are active; the resulting number is 64 + 32 + 8 + 2 = 106.
Now to find out whether a flag is active, we do the bitwise AND with that number and the flags value; lets check for e:
Since this result is not 0, we know that flag e is active.
cant find more good usage examples, sry appinv
There are two different ways of shifting: left shift (<<) and right shift (>>)
Shifting moves the binary digits of a number n places to the right/left
Right shifts discard rightmost bits when shifted away:
A left shift (x << n) is equivalent to (x * 2^n), while a right shift is equivalent to (x / 2^n) rounded down
In an arithmetic right shift, instead of inserting 0 bits, the most significant bit (sign bit) is copied.
Rotate no carry(edited)
Rotate no carry is another kind of shift, where discarded bits get moved to the other end:
10100101 RNC RIGHT 1 =
If you need more info go to https://en.wikipedia.org/wiki/Bitwise_operation now bye
written by @supersebi