// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public static class BitFun { public static void BitSet(byte[] ba, int ix) { ba[ix / 8] = (byte)(ba[ix / 8] | (1 << ix % 8)); } public static void BitClr(byte[] ba, int ix) { ba[ix / 8] = (byte)(ba[ix / 8] & ~(1 << ix % 8)); } public static bool BitTst(byte[] ba, int ix) { return (ba[ix / 8] & (1 << ix % 8)) != 0; } } public class Program { public static void Main(string[] args) { byte[] b = { 0, 255 }; Console.WriteLine(b[0].ToString("X2") + " " + b[1].ToString("X2")); for(int i = 0; i < 16; i++) Console.Write(BitFun.BitTst(b, i) ? '1' : '0'); Console.WriteLine(); BitFun.BitSet(b, 2); BitFun.BitClr(b, 10); Console.WriteLine(b[0].ToString("X2") + " " + b[1].ToString("X2")); for(int i = 0; i < 16; i++) Console.Write(BitFun.BitTst(b, i) ? '1' : '0'); Console.WriteLine(); } } }