### 11 releases

0.3.0 | Aug 30, 2021 |
---|---|

0.2.0 | Aug 28, 2021 |

0.1.9 | Aug 28, 2021 |

#**1094** in Algorithms

**36** downloads per month

Used in rotation

**MIT**license

5KB

50 lines

### gcd_bitwise

**Disclaimer: The code is not mine.**

The underlying code is part of the coreutils project. I have forked it for ease of use, for those who dont want to pull in big dependencies. I modified some parts for general use cases, eg. implementing generic types. This crate is dependency free.

### Update!

numeric types of arguments will be cast to instead of

`usize`**from now.**

`u32`

You can pass ,

`u8`**,**

`u16`

**,**

`u32`

**and**

`u64`

**numeric types into**

`usize`

**function. But please note that the 2 numbers that you pass must have the same type. Passing any signed type (like**

`gcd``(``)`

`i32`

) may give unexpected results. Please have a look at the **Quick Start**section below for examples.

### Some Notes

This code uses stein's algorithm, that replaces division with arithmetic shifts, comparisons, and subtraction, for optimization of performance. For more info please refer to this page.

### Quick Start

`use` `gcd_bitwise``::``interface``::`gcd`;`
`fn` `main``(``)`` ``{`
`//` For `u8` type
`let` num1`:` `u8` `=` `15``;`
`let` num2`:` `u8` `=` `51``;`
`let` gcd`:` `u8` `=` `gcd``(`num1`,` num2`)``;`
`println!``(``"`gcd: `{}``"``,` gcd`)``;` `//` 3
`//` For `usize` type
`let` num1`:` `usize` `=` `65535``;`
`let` num2`:` `usize` `=` `125``;`
`let` gcd`:` `usize` `=` `gcd``(`num1`,` num2`)``;`
`println!``(``"`gcd: `{}``"``,` gcd`)``;` `//` 5
`//` And on it goes...
`}`

If you find any problems dont hesitate to open an issue on github.

`Final Note`

`:`