Skip to content

Instantly share code, notes, and snippets.

@Coding-Enthusiast
Last active April 25, 2024 12:53

Revisions

  1. Coding-Enthusiast revised this gist Apr 13, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion BitwiseOperations.md
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,6 @@
    ### Mod by 2<sup>k</sup>
    x & (2<sup>k</sup>-1)

    Example: 20 % 16 = 20 & 15
    Example: 20 % 16 = 20 & 15
    ### Is x power of 2?
    (x != 0) && (x & (x - 1)) == 0
  2. Coding-Enthusiast created this gist Apr 12, 2019.
    33 changes: 33 additions & 0 deletions BitwiseOperations.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    ### AND (&)
    100
    101
    ---
    100
    ### OR (|)
    100
    101
    ---
    101
    ### XOR (^)
    100
    101
    ---
    001
    ### NOT (~)
    101
    ---
    010
    ### Shift (>>) and (<<)
    0001_0111 >> 3 = 0000_0010
    0001_0111 << 3 = 1011_1000
    # Arithmetic
    ### Multiply x by 2<sup>k</sup>
    x << k
    Example: 5 * 8 = 5 << 3
    ### Divide x by 2<sup>k</sup>
    x >> k
    Example: 20 / 16 = 20 >> 4
    ### Mod by 2<sup>k</sup>
    x & (2<sup>k</sup>-1)

    Example: 20 % 16 = 20 & 15