Skip to content

Instantly share code, notes, and snippets.

@imolorhe
Created March 28, 2020 22:16
Show Gist options
  • Select an option

  • Save imolorhe/7328a6a43f8e2e5ecc1bbde95b67e6ed to your computer and use it in GitHub Desktop.

Select an option

Save imolorhe/7328a6a43f8e2e5ecc1bbde95b67e6ed to your computer and use it in GitHub Desktop.

Revisions

  1. imolorhe created this gist Mar 28, 2020.
    93 changes: 93 additions & 0 deletions bitwise
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    1 << 2


    4 << 1 // bitwise left shift operator
    // d - 4
    // b - 100

    // >> n
    // << n => 2 ^ n

    5 << 1 // 10 => 5 * (2 ^ n)
    5 << 2 // 20 => 5 * (2 ^ n)

    8 >> 2 // 2
    // 1000 >> 10

    x << n // x * (2 ^ n)

    // 000000001
    // 000000100 << 2
    // 001000000 << n

    // config = { dark: true, privacy: true, loggedIn: true, bookmarked: true, archived: false };
    // config.dark = true;

    // dplba
    // 11110 => 30

    // const d_mask = 10000; // dark
    // const p_mask = 01000; // privacy
    // const l_mask = 00100; // loggedIn

    // Setting to true
    // -----
    // 11110
    // |
    // 10000
    // -----
    // 11110

    // Toggle
    // ------
    // 11110
    // ^
    // 10000
    // -----
    // 01110

    // _________________
    // 01110
    // |
    // 10000
    // -----
    // 01110

    // Setting to false
    // -----
    // 11110
    // &
    // 01111 (~ (10000))
    // -----
    // 01110 => 14

    // bitwise operators
    // AND, OR, XOR

    // &, |, ^

    // 1 & 0 => 0
    // 1 | 0 => 1

    // 1 ^ 1 => 0
    // 0 ^ 1 => 1
    // 1 ^ 0 => 1
    // 0 ^ 0 => 0



    parseInt(01110001, 2);
    parseInt('01110001', 2);

    x.toString(2);