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:

11010110

& 10111001

̅ ̅1̅0̅0̅1̅0̅0̅0̅0̅

(edited)

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)

11010110

| 10111001

̅ ̅1̅1̅1̅1̅1̅1̅1̅1̅

11010110

^ 10111001

̅ ̅0̅1̅1̅0̅1̅1̅1̅1̅

(edited)

The bitwise NOT (¬, represented as ~ in most programming languages) operateoperates on a single number, just like the logical NOT operates on a single bit:

¬ 11010110

̅ ̅0̅0̅1̅0̅1̅0̅0̅1̅

(edited)

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.

For example, regular expressions use flags: /regexpattern/flags is the syntax for regexes in javascript.

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:

01101010

& 00001000

= 00001000

Since this result is not 0, we know that flag e is active.

cant find more good usage examples, sry appinv

—

BITWISE SHIFTS

Logical shift

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

0010010110

<< 2

= 1001011000

Right shifts discard rightmost bits when shifted away:

0010010110

>> 2

= 0000100101

(edited)

A left shift (x << n) is equivalent to (x * 2^n), while a right shift is equivalent to (x / 2^n) rounded down

Arithmetic shifts

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 =

11010010

01101001

10110100

(edited)

If you need more info go to https://en.wikipedia.org/wiki/Bitwise_operation now bye

asm / assembly, computer architecture, Uncategorized