public BoolArray BinaryBitwiseOp(Func <bool, bool, bool> op, BoolArray ba, int start = 0) { for (int i = 0; i < ba.Count; i++) { if (start + i >= this.Count) { break; } this[start + i] = op(this[start + i], ba[i]); } return(this); }
public static BoolArray FromBinaryString(string bin, char[] trueChars = null) { if (trueChars == null) { trueChars = new char[] { '1', 'Y', 'y', 'T', 't' } } ; if (bin == null) { throw new ArgumentNullException("bin"); } BoolArray ba = new BoolArray(bin.Length); for (int i = 0; i < bin.Length; i++) { ba[i] = bin[i].In(trueChars); } return(ba); }
public byte[] ToBytes(int startBit = 0, int numBits = -1) { if (numBits == -1) { numBits = this._length - startBit; } BoolArray ba = GetBits(startBit, numBits); int nb = (numBits + 7) / 8; byte[] bb = new byte[nb]; for (int i = 0; i < ba.Count; i++) { if (!ba[i]) { continue; } int bp = 7 - (i % 8); bb[i / 8] = (byte)((int)bb[i / 8] | (1 << bp)); } return(bb); }
public static BoolArray FromHexString(string hex) { if (hex == null) { throw new ArgumentNullException("hex"); } List <bool> bits = new List <bool>(); for (int i = 0; i < hex.Length; i++) { int b = byte.Parse(hex[i].ToString(), NumberStyles.HexNumber); bits.Add((b >> 3) == 1); bits.Add(((b & 0x7) >> 2) == 1); bits.Add(((b & 0x3) >> 1) == 1); bits.Add((b & 0x1) == 1); } BoolArray ba = new BoolArray(bits.ToArray()); return(ba); }
public BoolArray Or(BoolArray or, int start = 0) { return(BinaryBitwiseOp((a, b) => (a | b), or, start)); }
public BoolArray And(BoolArray and, int start = 0) { return(BinaryBitwiseOp((a, b) => (a & b), and, start)); }
public BoolArray Xor(BoolArray xor, int start = 0) { return(BinaryBitwiseOp((a, b) => (a ^ b), xor, start)); }
public BoolArray Append(BoolArray addBits) { return(this.Append(addBits.ToArray())); }
public BoolArray(BoolArray srcBits) { this.InitializeFrom(srcBits.ToArray()); }