Reputation: 4723
I have a byte array of 32 bytes where the first 4 bits of each byte (0 to 3) represent the set or unset state of a number between 1 and 128. For example, If I am given the number 3, I need to set bit 2 in the first byte in the array. If I am given the number 9, I need to set bit 0 of the third byte in the array. The problem I have is finding a sensible way to do this in C#. I’m sure there must be a simple way to do it mathematically but so far haven’t been able to find a method. Whilst I scratch my head on this one I thought I’d see if anyone can give some advice.
--------- Update -------------------
Based on the answers given, I have produced the following function. This does exactly what I need. I may not have made it clear in my question what I needed but enough advice was given for me to find the right code.
// outputNumber = number passed into this function
byte[] bytes = new byte[32];
int bit = (outputNumber - 1) % 4;
byte byteSetting = (byte)(1 << bit);
bytes[(outputNumber - 1) / 4] |= byteSetting;
Upvotes: 1
Views: 5128
Reputation: 2540
Needed something similar. On a 64 bit system use ulongs (32 bit -> uint) instead of bytes. The performace difference is quite significant.
public struct BitField {
private ulong[] _Values;
private BitField(ulong[] values) {
_Values = values;
}
public static BitField New() {
return new BitField(new ulong[] { 0ul, 0ul });
}
public BitField Clone() {
return new BitField(new ulong[] { _Values[0], _Values[1] });
}
public void Clear() {
_Values[0] = ulong.MinValue;
_Values[1] = ulong.MinValue;
}
public void SetAll() {
_Values[0] = ulong.MaxValue;
_Values[1] = ulong.MaxValue;
}
public void AND_Combine(BitField bitField) {
_Values[0] &= bitField._Values[0];
_Values[1] &= bitField._Values[1];
}
public void OR_Combine(BitField bitField) {
_Values[0] |= bitField._Values[0];
_Values[1] |= bitField._Values[1];
}
public bool Intersects(BitField bitField) {
if ((_Values[0] & bitField._Values[0]) > 0) {
return true;
}
else {
if ((_Values[1] & bitField._Values[1]) > 0) {
return true;
}
else {
return false;
}
}
}
public bool this[int index] {
get {
if (index > 127 || index < 0) {
return false;
}
int item = index >> 6;
int bit = index % 64;
ulong compare = 1ul << bit;
return ((_Values[item] & compare) == compare);
}
set {
if (index >= 0 || index < 128) {
int item = index >> 6;
int bit = index % 64;
ulong compare = 1ul << bit;
if (value) {
_Values[item] |= compare;
}
else {
_Values[item] &= ~compare;
}
}
}
}
}
Upvotes: 1
Reputation: 111940
int byt = bitNumber / 4; // You could do byt = bitNumber >> 2
int bit = bitNumber % 4; // You could do bit = bitNumber & 3
bytes[byt] |= (byte)(1 << bit);
Where bytes
is your byte array.
To reset it:
bytes[byt] &= (byte)(byte.MaxValue ^ (1 << bit));
To read the value of a byte:
var res = bytes[byt] & (byte)(1 << bit)
(if you are interested, ^
is the xor operator)
Upvotes: 3
Reputation: 9492
You can set the bits in each byte in an array like this:
array[2] |= (byte)(1<<3); // set bit #4 / index 3 in array element #3 / index 2
You can clear a bit like this:
array[2] &= unchecked((byte)(~(1<<3))); // clear the same bit we set previously
Upvotes: 2