-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bit.cs
128 lines (110 loc) · 4.5 KB
/
Bit.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright (C) 2016 Frank Stinner
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>.
Dieses Programm ist freie Software. Sie können es unter den Bedingungen
der GNU General Public License, wie von der Free Software Foundation
veröffentlicht, weitergeben und/oder modifizieren, entweder gemäß
Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es
Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne
die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN
BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
Sie sollten ein Exemplar der GNU General Public License zusammen mit
diesem Programm erhalten haben. Falls nicht, siehe
<http://www.gnu.org/licenses/>.
*/
namespace GarminCore {
/// <summary>
/// Hilfsfunktionen um Bits zu setzen oder zu testen
/// </summary>
public class Bit {
/// <summary>
/// setzt ein Bit auf 0 oder 1 und liefert den neuen Wert
/// </summary>
/// <param name="v"></param>
/// <param name="bit">0..31</param>
/// <param name="set"></param>
/// <returns></returns>
static public uint Set(uint v, byte bit, bool set = true) {
if (bit >= 32)
throw new System.ArgumentException("Nur Bit 0 bis 31 erlaubt.", "bit");
if (set)
v |= (uint)(0x01 << bit);
else
v &= (uint)(~(0x01 << bit));
return v;
}
/// <summary>
/// setzt ein Bit auf 0 oder 1
/// </summary>
/// <param name="v"></param>
/// <param name="bit">0..31</param>
/// <param name="set"></param>
static public void Set(ref uint v, byte bit, bool set = true) {
v = Set(v, bit, set);
}
/// <summary>
/// testet, ob das Bit gesetzt ist
/// </summary>
/// <param name="v"></param>
/// <param name="bit"></param>
/// <returns></returns>
static public bool IsSet(uint v, byte bit) {
if (bit >= 32)
throw new System.ArgumentException("Nur Bit 0 bis 31 erlaubt.", "bit");
return (v & (0x01 << bit)) != 0;
}
/// <summary>
/// liefert <see cref="count"/> Bits ab Bit <see cref="from"/> (als Zahl)
/// </summary>
/// <param name="v"></param>
/// <param name="from">0..31</param>
/// <param name="count">Bitanzahl</param>
/// <returns></returns>
static public uint GetUInt(uint v, byte from, byte count) {
if (from >= 32)
throw new System.ArgumentException("Nur Bit 0 bis 31 erlaubt.", "from");
if (count == 0 || count > 32)
throw new System.ArgumentException("Nur 1 bis 32 erlaubt.", "count");
if (from + count > 32)
throw new System.ArgumentException("Bitanzahl ist zu groß.", "count");
v >>= from;
uint mask = 0x01;
while (--count > 0)
mask = 0x01 | (mask << 1);
return v & mask;
}
/// <summary>
/// setzt den Bitbereich mit der Zahl
/// </summary>
/// <param name="v"></param>
/// <param name="val">neue Bits</param>
/// <param name="from">0..31</param>
/// <param name="count">Bitanzahl</param>
/// <returns></returns>
static public uint SetUInt(uint v, uint val, byte from, byte count) {
if (from >= 32)
throw new System.ArgumentException("Nur Bit 0 bis 31 erlaubt.", "from");
if (count == 0 || count > 32)
throw new System.ArgumentException("Nur 1 bis 32 erlaubt.", "count");
if (from + count > 32)
throw new System.ArgumentException("Bitanzahl ist zu groß.", "count");
uint mask = 0x01;
while (--count > 0)
mask = 0x01 | (mask << 1);
val &= mask; // auf gültigen Bereich eingeschränkt
val <<= from;
mask <<= from;
return (v | mask) & val;
}
}
}