Skip to content

Instantly share code, notes, and snippets.

@alexandercerutti
Last active December 28, 2021 23:19
Show Gist options
  • Save alexandercerutti/addb682f226951eaef71a27d297f3729 to your computer and use it in GitHub Desktop.
Save alexandercerutti/addb682f226951eaef71a27d297f3729 to your computer and use it in GitHub Desktop.
bitwise-article
/**
* In JS, we can write bits by using prefix `0b`.
* In this example, bits are separated by a space just to make
* it simple to apply grid reading.
*
* Grid Reading means picking one column at time and reasoning only
* on that column. This will be helpful in the article.
*
* Please, also note that in JS, 0b numbers can use up to 32 bits (2^32 = 4294967296).
*/
const N = 0b00101001;
const M = 0b10110101;
/** N AND M **/
N & M
/**
* =======================
* N : 0b 0 0 1 0 1 0 0 1
* M : 0b 1 0 1 1 0 1 0 1
* =======================
* O : 0b 0 0 1 0 0 0 0 1
*/
/** N OR M **/
N | M
/**
* =======================
* N : 0b 0 0 1 0 1 0 0 1
* M : 0b 1 0 1 1 0 1 0 1
* =======================
* O : 0b 1 0 1 1 1 1 0 1
*/
/** NOT N **/
~N
/**
* =======================
* N : 0b 0 0 1 0 1 0 0 1
* =======================
* O : 0b 1 1 0 1 0 1 1 0
*/
/** N XOR M **/
N ^ M
/**
* =======================
* N : 0b 0 0 1 0 1 0 0 1
* M : 0b 1 0 1 1 0 1 0 1
* =======================
* O : 0b 1 0 0 1 1 1 0 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment