Skip to content

Instantly share code, notes, and snippets.

@synthetic-intelligence
Forked from dideler/bitwise-operators.md
Created January 27, 2021 09:35
Show Gist options
  • Select an option

  • Save synthetic-intelligence/68025f01461a8978e9740372b50fae75 to your computer and use it in GitHub Desktop.

Select an option

Save synthetic-intelligence/68025f01461a8978e9740372b50fae75 to your computer and use it in GitHub Desktop.

Revisions

  1. @dideler dideler revised this gist Apr 12, 2012. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions bitwise-operators.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ or you will get unexpected results.
    x = x >> 1; // x = x / 2
    x = x >> 3; // x = x / 8

    ###### Swap integers without a temporary variable
    ##### Swap integers without a temporary variable
    a ^= b; // int temp = b
    b ^= a; // b = a
    a ^= b; // a = temp
    @@ -23,27 +23,27 @@ or you will get unexpected results.
    i = -~i; // i++
    i = ~-i; // i--

    ###### Sign flipping
    ##### Sign flipping
    i = ~i + 1; // or
    i = (i ^ -1) + 1; // i = -i

    ###### Modulo operation if divisor is power of 2
    x = 131 & (4 - 1); // x = 131 % 4

    ###### Check if an integer is even or odd
    ##### Check if an integer is even or odd
    (i & 1) == 0; // (i % 2) == 0

    ###### Equality check
    (a^b) == 0; // a == b

    ###### Absolute value
    ##### Absolute value
    x < 0 ? -x : x; // abs(x)
    (x ^ (x >> 31)) - (x >> 31) // abs(x)

    ###### Equal sign check (both ints are pos or neg)
    a ^ b >= 0; // a * b > 0

    ###### Rounding, ceiling, flooring
    ##### Rounding, ceiling, flooring
    (x + 0.5) >> 0; // round(x)
    (x + 1) >> 0; // ceil(x)
    x >> 0; // floor(x)
  2. @dideler dideler created this gist Apr 12, 2012.
    49 changes: 49 additions & 0 deletions bitwise-operators.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    Inspired by [this article](http://lab.polygonal.de/?p=81).
    Neat tricks for speeding up integer computations.

    Note: `cin.sync_with_stdio(false);` disables synchronous IO and gives you a performance boost.
    If used, you should only use `cin` for reading input
    (don't use both `cin` and `scanf` when sync is disabled, for example)
    or you will get unexpected results.

    ##### Multiply by a power of 2
    x = x << 1; // x = x * 2
    x = x << 6; // x = x * 64

    ###### Divide by a power of 2
    x = x >> 1; // x = x / 2
    x = x >> 3; // x = x / 8

    ###### Swap integers without a temporary variable
    a ^= b; // int temp = b
    b ^= a; // b = a
    a ^= b; // a = temp

    ###### Increment / Decrement (slower but good for obfuscating)
    i = -~i; // i++
    i = ~-i; // i--

    ###### Sign flipping
    i = ~i + 1; // or
    i = (i ^ -1) + 1; // i = -i

    ###### Modulo operation if divisor is power of 2
    x = 131 & (4 - 1); // x = 131 % 4

    ###### Check if an integer is even or odd
    (i & 1) == 0; // (i % 2) == 0

    ###### Equality check
    (a^b) == 0; // a == b

    ###### Absolute value
    x < 0 ? -x : x; // abs(x)
    (x ^ (x >> 31)) - (x >> 31) // abs(x)

    ###### Equal sign check (both ints are pos or neg)
    a ^ b >= 0; // a * b > 0

    ###### Rounding, ceiling, flooring
    (x + 0.5) >> 0; // round(x)
    (x + 1) >> 0; // ceil(x)
    x >> 0; // floor(x)