-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteger.cs
53 lines (49 loc) · 1.8 KB
/
Integer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
namespace Amoenus.NiceInteger
{
/// <summary>
/// Nice helpers for Integers
/// </summary>
public static class Integer
{
/// <summary>
/// Determines whether dividend is divisible by the specified divisor.
/// </summary>
/// <param name="dividend">The dividend.</param>
/// <param name="divisor">The divisor.</param>
/// <returns>
/// <c>true</c> if is divisible by the specified divisor; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="System.DivideByZeroException"></exception>
public static bool IsDivisibleBy(this int dividend, int divisor)
{
if (divisor == 0)
throw new DivideByZeroException();
return ((divisor & (~divisor + 1)) == divisor
? dividend & (divisor - 1)
: dividend % divisor) == 0;
}
/// <summary>
/// Determines whether dividend is odd number.
/// </summary>
/// <param name="dividend">The dividend.</param>
/// <returns>
/// <c>true</c> if is divisible by the specified divisor; otherwise, <c>false</c>.
/// </returns>
public static bool IsOdd(this int dividend)
{
return (dividend & 0x01) != 0;
}
/// <summary>
/// Determines whether dividend is even number.
/// </summary>
/// <param name="dividend">The dividend.</param>
/// <returns>
/// <c>true</c> if is divisible by the specified divisor; otherwise, <c>false</c>.
/// </returns>
public static bool IsEven(this int dividend)
{
return (dividend & 0x01) == 0;
}
}
}