Skip to content

Commit d8dfdc6

Browse files
authored
Include BitStream code internally (#6)
1 parent 2e5b849 commit d8dfdc6

File tree

8 files changed

+1607
-2
lines changed

8 files changed

+1607
-2
lines changed

Fmod5Sharp/BitStreams/Bit.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
3+
namespace BitStreams
4+
{
5+
[Serializable]
6+
internal struct Bit
7+
{
8+
private readonly byte value;
9+
10+
private Bit(int value)
11+
{
12+
this.value = (byte)(value & 1);
13+
}
14+
15+
public static implicit operator Bit(int value)
16+
{
17+
return new Bit(value);
18+
}
19+
20+
public static implicit operator Bit(bool value)
21+
{
22+
return new Bit(value ? 1 : 0);
23+
}
24+
25+
public static implicit operator int (Bit bit)
26+
{
27+
return bit.value;
28+
}
29+
30+
public static implicit operator byte (Bit bit)
31+
{
32+
return (byte)bit.value;
33+
}
34+
35+
public static implicit operator bool (Bit bit)
36+
{
37+
return bit.value == 1;
38+
}
39+
40+
public static Bit operator &(Bit x, Bit y)
41+
{
42+
return x.value & y.value;
43+
}
44+
45+
public static Bit operator |(Bit x, Bit y)
46+
{
47+
return x.value | y.value;
48+
}
49+
50+
public static Bit operator ^(Bit x, Bit y)
51+
{
52+
return x.value ^ y.value;
53+
}
54+
55+
public static Bit operator ~(Bit bit)
56+
{
57+
return (~(bit.value) & 1);
58+
}
59+
60+
public static implicit operator string (Bit bit)
61+
{
62+
return bit.value.ToString();
63+
}
64+
65+
public int AsInt()
66+
{
67+
return this.value;
68+
}
69+
70+
public bool AsBool()
71+
{
72+
return this.value == 1;
73+
}
74+
}
75+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
namespace BitStreams
2+
{
3+
internal static class BitExtensions
4+
{
5+
6+
#region GetBit
7+
8+
public static Bit GetBit(this byte n, int index)
9+
{
10+
return n >> index;
11+
}
12+
13+
public static Bit GetBit(this sbyte n, int index)
14+
{
15+
return n >> index;
16+
}
17+
18+
public static Bit GetBit(this short n, int index)
19+
{
20+
return n >> index;
21+
}
22+
23+
public static Bit GetBit(this ushort n, int index)
24+
{
25+
return n >> index;
26+
}
27+
28+
public static Bit GetBit(this int n, int index)
29+
{
30+
return n >> index;
31+
}
32+
33+
public static Bit GetBit(this uint n, int index)
34+
{
35+
return (byte)(n >> index);
36+
}
37+
38+
public static Bit GetBit(this long n, int index)
39+
{
40+
return (byte)(n >> index);
41+
}
42+
43+
public static Bit GetBit(this ulong n, int index)
44+
{
45+
return (byte)(n >> index);
46+
}
47+
48+
#endregion
49+
50+
#region CircularShift
51+
52+
public static byte CircularShift(this byte n, int bits, bool leftShift)
53+
{
54+
if (leftShift)
55+
{
56+
n = (byte)(n << bits | n >> (8 - bits));
57+
}
58+
else
59+
{
60+
n = (byte)(n >> bits | n << (8 - bits));
61+
}
62+
return n;
63+
}
64+
65+
public static sbyte CircularShift(this sbyte n, int bits, bool leftShift)
66+
{
67+
if (leftShift)
68+
{
69+
n = (sbyte)(n << bits | n >> (8 - bits));
70+
}
71+
else
72+
{
73+
n = (sbyte)(n >> bits | n << (8 - bits));
74+
}
75+
return n;
76+
}
77+
78+
public static short CircularShift(this short n, int bits, bool leftShift)
79+
{
80+
if (leftShift)
81+
{
82+
n = (short)(n << bits | n >> (16 - bits));
83+
}
84+
else
85+
{
86+
n = (short)(n >> bits | n << (16 - bits));
87+
}
88+
return n;
89+
}
90+
91+
public static ushort CircularShift(this ushort n, int bits, bool leftShift)
92+
{
93+
if (leftShift)
94+
{
95+
n = (ushort)(n << bits | n >> (16 - bits));
96+
}
97+
else
98+
{
99+
n = (ushort)(n >> bits | n << (16 - bits));
100+
}
101+
return n;
102+
}
103+
104+
public static int CircularShift(this int n, int bits, bool leftShift)
105+
{
106+
if (leftShift)
107+
{
108+
n = (n << bits | n >> (32 - bits));
109+
}
110+
else
111+
{
112+
n = (n >> bits | n << (32 - bits));
113+
}
114+
return n;
115+
}
116+
117+
public static uint CircularShift(this uint n, int bits, bool leftShift)
118+
{
119+
if (leftShift)
120+
{
121+
n = (uint)(n << bits | n >> (32 - bits));
122+
}
123+
else
124+
{
125+
n = (uint)(n >> bits | n << (32 - bits));
126+
}
127+
return n;
128+
}
129+
130+
public static long CircularShift(this long n, int bits, bool leftShift)
131+
{
132+
if (leftShift)
133+
{
134+
n = (n << bits | n >> (64 - bits));
135+
}
136+
else
137+
{
138+
n = (n >> bits | n << (64 - bits));
139+
}
140+
return n;
141+
}
142+
143+
public static ulong CircularShift(this ulong n, int bits, bool leftShift)
144+
{
145+
if (leftShift)
146+
{
147+
n = (ulong)(n << bits | n >> (64 - bits));
148+
}
149+
else
150+
{
151+
n = (ulong)(n >> bits | n << (64 - bits));
152+
}
153+
return n;
154+
}
155+
156+
#endregion
157+
158+
#region Reverse
159+
160+
public static byte ReverseBits(this byte b)
161+
{
162+
return (byte)(((b & 1) << 7) + ((((b >> 1) & 1) << 6)) + (((b >> 2) & 1) << 5) + (((b >> 3) & 1) << 4) + (((b >> 4) & 1) << 3) +(((b >> 5) & 1) << 2) +(((b >> 6) & 1) << 1) + ((b >> 7)&1));
163+
}
164+
165+
#endregion
166+
}
167+
}

0 commit comments

Comments
 (0)