forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitwiseHelpers.cs
More file actions
114 lines (90 loc) · 3.11 KB
/
BitwiseHelpers.cs
File metadata and controls
114 lines (90 loc) · 3.11 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Net;
namespace Microsoft.ClearScript.Util
{
internal static class BitwiseHelpers
{
#region bitwise operations
public static byte And(this byte value, byte mask)
{
var tempValue = value;
tempValue &= mask;
return tempValue;
}
public static byte Or(this byte value, byte mask)
{
var tempValue = value;
tempValue |= mask;
return tempValue;
}
public static byte Xor(this byte value, byte mask)
{
var tempValue = value;
tempValue ^= mask;
return tempValue;
}
public static bool Has(this byte value, byte mask)
{
return value.And(mask) != 0;
}
public static bool HasAll(this byte value, byte mask)
{
return value.And(mask) == mask;
}
#endregion
#region signed/unsigned conversion
public static short ToSigned(this ushort value)
{
return BitConverter.ToInt16(BitConverter.GetBytes(value), 0);
}
public static int ToSigned(this uint value)
{
return BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
}
public static long ToSigned(this ulong value)
{
return BitConverter.ToInt64(BitConverter.GetBytes(value), 0);
}
public static ushort ToUnsigned(this short value)
{
return BitConverter.ToUInt16(BitConverter.GetBytes(value), 0);
}
public static uint ToUnsigned(this int value)
{
return BitConverter.ToUInt32(BitConverter.GetBytes(value), 0);
}
public static ulong ToUnsigned(this long value)
{
return BitConverter.ToUInt64(BitConverter.GetBytes(value), 0);
}
#endregion
#region network byte order support
public static ushort ToHostUInt16(this byte[] bytes, int index = 0)
{
return IPAddress.NetworkToHostOrder(BitConverter.ToInt16(bytes, index)).ToUnsigned();
}
public static uint ToHostUInt32(this byte[] bytes, int index = 0)
{
return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, index)).ToUnsigned();
}
public static ulong ToHostUInt64(this byte[] bytes, int index = 0)
{
return IPAddress.NetworkToHostOrder(BitConverter.ToInt64(bytes, index)).ToUnsigned();
}
public static byte[] ToNetworkBytes(this ushort value)
{
return BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value.ToSigned()));
}
public static byte[] ToNetworkBytes(this uint value)
{
return BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value.ToSigned()));
}
public static byte[] ToNetworkBytes(this ulong value)
{
return BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value.ToSigned()));
}
#endregion
}
}