Skip to content

Commit

Permalink
Bitwise operator (aalhour#59)
Browse files Browse the repository at this point in the history
* Bitwise operator

Using Bitwise operator (&) "r" will recives the modulus result like modulus operator "%" is doing, but is more efficient and fast.

* Comment Bitwise operator

Added a comment to explaing why use bitwise operator.
  • Loading branch information
raphaelmdcoelho authored and aalhour committed Feb 7, 2018
1 parent c04aa06 commit 7d41054
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Algorithms/Numeric/GreatestCommonDivisor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ public static uint FindGCD(uint a, uint b)
return a;

uint _a = a, _b = b;
uint r = _a % _b;

//Bitwise operator '&' works on individual bits of each value
//result is 0 or 1
//it works like a modulus operator '%' but is more efficient
uint r = _a & _b;

while(r != 0)
{
_a = _b;
_b = r;
r = _a % _b;
r = _a & _b;
}

return _b;
Expand Down

0 comments on commit 7d41054

Please sign in to comment.