forked from sleepy-monax/hevadea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueExtension.cs
38 lines (31 loc) · 1.04 KB
/
ValueExtension.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
using System;
namespace Hevadea.Framework.Extension
{
public static class ValueExtension
{
public static byte WriteBit(this byte value, byte selectedBit, bool bit)
{
if (bit) return (byte) (value | (1 << selectedBit));
return (byte) (value & ~(1 << selectedBit));
}
public static int WriteBit(this int value, byte selectedBit, bool bit)
{
if (bit) return value | (1 << selectedBit);
return value & ~(1 << selectedBit);
}
public static bool ReadBit(this byte value, int selectedBit)
{
return (value & (1 << selectedBit)) != 0;
}
public static bool ReadBit(this int value, int selectedBit)
{
return (value & (1 << selectedBit)) != 0;
}
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
}
}