Basically, I've been searching for days now how to work with bits and bitmasks and learned some pretty interesting stuff, mainly about how computers work. The question is a result of me trying to work with the Minecraft
protocol. Part of that is bitmasks on a byte. Through my searching I think I've got this concept down, but I'm not entirely sure. I wasn't sure how to test my work either, because I haven't found a way to represent the byte as a string (maybe I'm just slow and forgetful).
Simply put, here is my code for setting a bit based on a bit mask. Had I done this correctly?
// BitMask is based on Enum byte value (0x00, 0x01, 0x02, 0x04, 0x08, etc.)
public void SetBitOn(Enum value, bool on)
{
// Ignore this part, just part of the software I'm working on
// to determine what type is being worked on
if (Type != EntityMetadataType.Byte)
throw new ArgumentException("Type must be of byte");
byte mask = Convert.ToByte(value);
byte val = (byte) Value;
if (on)
{
val &= (byte) ~mask;
}
else
{
val |= (byte) mask;
}
Value = val;
}