private void gelButton2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            int nCount = listBox1.Items.Count;
            minArray = new Minterm[nCount];
            for (int nIndex = 0; nIndex < nCount; nIndex++)
            {
                minArray[nIndex] = new Minterm(
                Convert.ToInt32(listBox1.Items[nIndex].ToString()));
            }

            minArray = SortMintermByWeight(minArray);
            for (int nIndex = 0; nIndex < nCount; nIndex++)
            {

                listView1.Items.Add(minArray[nIndex].StringValue);

                if (0 == minArray[nIndex].GetWeight() % 2)
                {
                    listView1.Items[nIndex].ForeColor = Color.Yellow;
                    listView1.Items[nIndex].BackColor = Color.Black;
                }
                else
                {
                    listView1.Items[nIndex].ForeColor = Color.Black;
                    listView1.Items[nIndex].BackColor = Color.Yellow;
                }
            }
        }
        public Minterm[] SortMintermByWeight(Minterm[] minArray)
        {
            int nValue = -1;
            int nLowerIndex = 0;

            Minterm mint = new Minterm(0);
            for (int nIndex = 0; nIndex < minArray.Length - 1; nIndex++)
            {
                for (int nIndexB = nIndex + 1; nIndexB < minArray.Length; nIndexB++)
                {
                    if (minArray[nIndex].GetWeight() > minArray[nIndexB].GetWeight())
                    {
                        mint = minArray[nIndex];
                        minArray[nIndex] = minArray[nIndexB];
                        minArray[nIndexB] = mint;
                    }
                }
            }
            return minArray;
        }
        public void ReduceEncryptionEquations()
        {
            if (encryptedMsg.GetBitState(0) == true)
            {
                string id1 = "".PadLeft(VariableIDLength, '0');
                string id2 = (dataSize).ToString().PadLeft(VariableIDLength, '0');
                for (int i = 0; i < encriptionEqs.Count(); i++)
                {
                    encriptionEqs[i].SubIn(id1, true);
                    encriptionEqs[i].SubIn(id2, true);
                }
            }
            if (encryptedMsg.GetBitState(dataSize * 2 - 2) == true)
            {
                string id1 = (dataSize - 1).ToString().PadLeft(VariableIDLength, '0');
                string id2 = (dataSize * 2 - 1).ToString().PadLeft(VariableIDLength, '0');
                for (int i = 0; i < encriptionEqs.Count(); i++)
                {
                    encriptionEqs[i].SubIn(id1, true);
                    encriptionEqs[i].SubIn(id2, true);
                }
            }

            for (int i = 1; i < dataSize; i++)
            {
                for (int j = i + 1; j < dataSize * 2 - 1; j++)
                {
                    Minterm mt = new Minterm();
                    ANFEquation a = new ANFEquation();
                    mt.AddAtom(encriptionEqs[i].DeepCopy());
                    mt.AddAtom(encriptionEqs[j].DeepCopy());
                    a.AddAtom(mt);
                    a = a.Extract(encriptionEqs[i]);
                    if (a != null)
                    {
                        encriptionEqs[j] = a;
                    }
                }
            }

            for (int i = dataSize * 2 - 2; i >= dataSize; i--)
            {
                for (int j = i - 1; j >= 0; j--)
                {
                    Minterm mt = new Minterm();
                    ANFEquation a = new ANFEquation();
                    mt.AddAtom(encriptionEqs[i].DeepCopy());
                    mt.AddAtom(encriptionEqs[j].DeepCopy());
                    a.AddAtom(mt);
                    a = a.Extract(encriptionEqs[i]);
                    if (a != null)
                    {
                        encriptionEqs[j] = a;
                    }
                }
            }
        }
        public EquationSetSolver(string EncryptedHexMsg, int DataSize)
        {
            this.dataSize = DataSize;
            this.VariableIDLength = (int)Math.Ceiling(Math.Log10(DataSize * 2));

            if (EncryptedHexMsg == null || EncryptedHexMsg == string.Empty)
            {
                EncryptedHexMsg = "000073af 00000000 000073af 00000000 000073af 00000000 000073af 00000000 000073af 00000000 000073af 00000000 000073af 00000000 000073af 00000000";
                //rawData = "73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af";
            }

            {
                int maxIndex = (DataSize / 16 > 0) ? DataSize / 16 : 1;
                EncryptedHexMsg = EncryptedHexMsg.Replace(" ", "").PadLeft(8, '0');
                this.encryptedMsg = new uint[maxIndex];
                for (int i = 0; i < maxIndex; i++)
                {
                    encryptedMsg[i] = uint.Parse(EncryptedHexMsg.Substring(i * 8, 8), System.Globalization.NumberStyles.HexNumber);
                }
            }
            encriptionEqs = new ANFEquation[DataSize * 2 - 1];

            // this next set of 4 repeat loops (nested included) is just a faster way of creating a map of the
            // operations done to each bit to end up with the encripted bit located at the same index as the index
            // of xorEq[index].  Faster compared to mapping and getting rid of duplicate procedures of the algorithm
            // that was provided to us by Nintendo/Alpha Centari.
            // The variable IDs are equal to the unencrypted bit index.
            for (int ebitIndex = 0; ebitIndex < DataSize; ebitIndex++)
            {
                var tmpEq = new ANFEquation();
                encriptionEqs[ebitIndex] = tmpEq;
                for (int termIndex = 0; termIndex <= ebitIndex; termIndex++)
                {
                    //int[] tmp = new int[2] { termIndex + 1, DataSize + ebitIndex - termIndex + 1 };
                    Minterm mt = new Minterm();
                    mt.AddAtom(new LogicVariable((termIndex).ToString().PadLeft(VariableIDLength, '0')));
                    mt.AddAtom(new LogicVariable((DataSize + ebitIndex - termIndex).ToString().PadLeft(VariableIDLength, '0')));
                    tmpEq.AddAtom(mt);
                }
                if (!encryptedMsg.GetBitState(ebitIndex))
                {
                    tmpEq.AddAtom(LogicConstantValue.True);
                }
            }
            for (int ebitIndex = DataSize * 2 - 2; ebitIndex >= DataSize; ebitIndex--)
            {
                var tmpEq = new ANFEquation();
                encriptionEqs[ebitIndex] = tmpEq;
                for (int termIndex = 0; termIndex <= DataSize * 2 - ebitIndex - 2; termIndex++)
                {
                    //int[] tmp = new int[2] { ebitIndex - DataSize + 2 + termIndex, DataSize * 2 - termIndex };
                    Minterm mt = new Minterm();
                    mt.AddAtom(new LogicVariable((ebitIndex - DataSize + 1 + termIndex).ToString().PadLeft(VariableIDLength, '0')));
                    mt.AddAtom(new LogicVariable((DataSize * 2 - termIndex - 1).ToString().PadLeft(VariableIDLength, '0')));
                    tmpEq.AddAtom(mt);
                }
                if (!encryptedMsg.GetBitState(ebitIndex))
                {
                    tmpEq.AddAtom(LogicConstantValue.True);
                }
            }
        }
        public string FullExpand()
        {
            if (encryptedMsg.GetBitState(0) == true)
            {
                string id1 = "".PadLeft(VariableIDLength, '0');
                string id2 = (dataSize).ToString().PadLeft(VariableIDLength, '0');
                for (int i = 0; i < encriptionEqs.Count(); i++)
                {
                    encriptionEqs[i].SubIn(id1, true);
                    encriptionEqs[i].SubIn(id2, true);
                }
            }
            if (encryptedMsg.GetBitState(dataSize * 2 - 2) == true)
            {
                string id1 = (dataSize - 1).ToString().PadLeft(VariableIDLength, '0');
                string id2 = (dataSize * 2 - 1).ToString().PadLeft(VariableIDLength, '0');
                for (int i = 0; i < encriptionEqs.Count(); i++)
                {
                    encriptionEqs[i].SubIn(id1, true);
                    encriptionEqs[i].SubIn(id2, true);
                }
            }

            ANFEquation final = new ANFEquation();

            final.AddAtom(encriptionEqs[0].DeepCopy());

            for (int i = 1; i < dataSize * 2 - 1; i++)
            {
                Minterm mt = new Minterm();
                mt.AddAtom(final);
                mt.AddAtom(encriptionEqs[i].DeepCopy());
                final = (ANFEquation)mt.Expand();
            }

            return final.ToString();
        }