using System;
using System.Collections.Generic;
using System.Text;
public class BitState
{
public BitState()
{
}
public BitState(byte aValue)
{
Value = aValue;
}
public byte Value = 0;
public void AddBit(int bitNumber)
{
// Binary or
Value = (byte)(Value | GetBit(bitNumber));
}
public bool HasBit(int bitNumber)
{
// Binary and
return (Value & GetBit(bitNumber)) == GetBit(bitNumber);
}
public byte GetBit(int bitNumber)
{
// Shift left
return (byte)(1 << bitNumber);
}
public void ToggleBit(int bitNumber)
{
// Binary xor
Value = (byte)(Value ^ GetBit(bitNumber));
}
public void RemoveBit(int bitNumber)
{
// Get bit to remove => invert => apply AND
Value = (byte)(Value & ~GetBit(bitNumber));
}
public void InvertBits()
{
Value = unchecked((byte)~Value);
}
public void Clear()
{
Value = 0;
}
public void SetAll()
{
Value = 255;
//Value = 0;
//InvertBits();
}
public override string ToString()
{
return Convert.ToString(this.Value, 2).PadLeft(8, '0');
}
public bool[] AsBoolArray()
{
int sizeInBits = 8;
bool[] boolArray = new bool[sizeInBits];
for (int i = 0; i < sizeInBits; i++)
boolArray[i] = HasBit(i);
return boolArray;
}
}
13000cookie-checkC# Modify a byte by bit