Each bit should be thought of as a “switch” that is either on or off, or, the bit is either set or not set. As a matter of fact, this precisely describes what a CPU does at the lowest level – flipping billions and billions (and counting!) of switches/bits.

Perhaps, and that is a big perhaps, an easier way to start thinking binary arithmetic is to think of a binary 1 as a decimal 5.

## Bitwise operations in JavaScript

Refer to MDN Web Docs – Bitwise operators.

It is worth specific noting that:

- JavaScript handles bitwise operations in 32-bit, even though leading 0 bits may not be displayed.
- It appears that after all but zero-fill right shift operations, JavaScript would evaluate the resulting binary number using two’s complement for signed number representation.
- The above signed evaluation does not appear to happen when
`parseInt()`

is called with radix 2. - The above means that if bitwise operations are performed on a binary number whose 32th bit is 1, the resulting decimal representation may not be what a person might naively expect. For this reason, where requirements permit, a person should try to think of bitwise operations only in the binary context.

## What happens when a bitwise operation is performed

### & (AND)

- 0: Unset a bit
- 1: No change to the bit

### | (OR)

- 0: No change to the bit
- 1: Set a bit

### ^ (XOR)

- 0: No change to the bit
- 1: Flip a bit

### + 1

Flip the right most 0 and everything to the right of that bit.

### – 1

Flip the right most 1 and everything to the right of that bit.

## To be continued…

(These are the most important details I have been able to gather so far. Remember to add more if more is discovered.)

## Additional resources

- Bitwise Algorithms – GeeksforGeeks (Be careful with this one though. While the concepts/ideas they describe are sound and rather comprehensive, at least one, if not more, of their articles seems to be filled with minor mistakes.)