Exemplo n.º 1
0
        //private void subtract(object sender, EventArgs e)
        //{
        //    MessageBox.Show("Não implementado");
        //}


        private void subtract(object sender, EventArgs e)
        {
            IEEE754 patternIEEE = new IEEE754();

            double v1 = !string.IsNullOrEmpty(valueOne.Text) ? double.Parse(valueOne.Text.Replace(".", ",")) : 0d;
            double v2 = !string.IsNullOrEmpty(valueTwo.Text) ? double.Parse(valueTwo.Text.Replace(".", ",")) : 0d;

            string    nBin1 = patternIEEE.FloatToBinary(float.Parse(valueOne.Text)).Replace(" ", "");
            string    nBin2 = patternIEEE.FloatToBinary(float.Parse(valueTwo.Text)).Replace(" ", "");
            string    mantissaBit1 = "", mantissaBit2 = "";
            Ula24Bits ula24Bits;

            if (v1 < 0)
            {
                nBin1        = nBin1[0] + nBin1.Substring(1, 8) + nBin1.Substring(9, 23).Replace('0', 'x');
                nBin1        = nBin1[0] + nBin1.Substring(1, 8) + nBin1.Substring(9, 23).Replace('1', '0');
                nBin1        = nBin1[0] + nBin1.Substring(1, 8) + nBin1.Substring(9, 23).Replace('x', '1');
                ula24Bits    = new Ula24Bits(arrayParse(nBin1.Substring(9, 23), 24), arrayParse("1".PadLeft(24, '0'), 24), 3, 0);
                mantissaBit1 = string.Join(string.Empty, ula24Bits.getSaidas());
                mantissaBit1 = nBin1[0] + nBin1.Substring(1, 8) + mantissaBit1.Substring(0, 23);
            }

            nBin2        = nBin2[0] + nBin2.Substring(1, 8) + nBin2.Substring(9, 23).Replace('0', 'x');
            nBin2        = nBin2[0] + nBin2.Substring(1, 8) + nBin2.Substring(9, 23).Replace('1', '0');
            nBin2        = nBin2[0] + nBin2.Substring(1, 8) + nBin2.Substring(9, 23).Replace('x', '1');
            ula24Bits    = new Ula24Bits(arrayParse(nBin2.Substring(9, 23), 24), arrayParse("1".PadLeft(24, '0'), 24), 3, 0);
            mantissaBit2 = string.Join(string.Empty, ula24Bits.getSaidas());
            mantissaBit2 = nBin2[0] + nBin2.Substring(1, 8) + mantissaBit2.Substring(0, 23);


            if (mantissaBit1 == "")
            {
                mantissaBit1 = nBin1;
            }
            string result = this.operation(mantissaBit1, mantissaBit2);

            this.setResultFields(result);
        }
Exemplo n.º 2
0
        private string operation(string nBin1, string nBin2)
        {
            int signal1 = int.Parse(nBin1[0].ToString());
            int signal2 = int.Parse(nBin2[0].ToString());

            double v1 = !string.IsNullOrEmpty(valueOne.Text) ? double.Parse(valueOne.Text.Replace(".", ",")) : 0d;
            double v2 = !string.IsNullOrEmpty(valueTwo.Text) ? double.Parse(valueTwo.Text.Replace(".", ",")) : 0d;


            string signalBitBin1 = nBin1.Substring(0, 1);
            string exponentBin1  = nBin1.Substring(1, 8);
            int    expoBin1      = Convert.ToInt32(exponentBin1, 2);
            int    lengthBin1    = expoBin1 - 127;
            string fractionBin1  = "1" + nBin1.Substring(9, 23);

            string signalBitBin2 = nBin2.Substring(0, 1);
            string exponentBin2  = nBin2.Substring(1, 8);
            int    expoBin2      = Convert.ToInt32(exponentBin2, 2);
            int    lengthBin2    = expoBin2 - 127;
            string fractionBin2  = "1" + nBin2.Substring(9, 23);

            this.currentExpo = exponentBin1;
            if (v1 != 0 && v2 == 0)
            {
                return(string.Join(string.Empty, new Ula(signal1, signal2, 3, 0).getSaidaUla()) + exponentBin1 + nBin1.Substring(9, 23));
            }
            else if (v2 != 0 && v1 == 0)
            {
                return(string.Join(string.Empty, new Ula(signal1, signal2, 3, 0).getSaidaUla()) + exponentBin2 + nBin2.Substring(9, 23));
            }
            else if (lengthBin1 > lengthBin2)
            {
                int   diff   = lengthBin1 - lengthBin2;
                int[] result = new Ula8Bits(arrayParse(exponentBin1, 8), arrayParse(diff.ToString("2"), 8), 3, 0).getSaidas();
                this.currentExpo = string.Join(string.Empty, result);
                string zeros = "".PadLeft(diff, '0');
                fractionBin2 = zeros + fractionBin2.Substring(0, (24 - zeros.Length));
            }
            else if (lengthBin1 < lengthBin2)
            {
                int   diff   = lengthBin2 - lengthBin1;
                int[] result = new Ula8Bits(arrayParse(exponentBin2, 8), arrayParse(diff.ToString("2"), 8), 3, 0).getSaidas();
                this.currentExpo = string.Join(string.Empty, result);
                string zeros = "".PadLeft(diff, '0');
                fractionBin1 = zeros + fractionBin1.Substring(0, (24 - zeros.Length));
            }
            Ula24Bits ula24Bits = new Ula24Bits(arrayParse(fractionBin1, 24), arrayParse(fractionBin2, 24), 3, 0);
            string    mantissa  = string.Join(string.Empty, ula24Bits.getSaidas());

            if (ula24Bits.getCarryOut() == 1)
            {
                int[] bitToNormalize = new int[8] {
                    0, 0, 0, 0, 0, 0, 0, 1
                };
                int[] finalResult = new Ula8Bits(arrayParse(this.currentExpo, 8), bitToNormalize, 3, 0).getSaidas();
                this.currentExpo = string.Join(string.Empty, finalResult);
                mantissa         = mantissa.Substring(0, 23);
            }
            else
            {
                mantissa = mantissa.Substring(1, 23);
            }

            return(string.Join(string.Empty, new Ula(signal1, signal2, 3, 0).getSaidaUla()) + this.currentExpo + mantissa.Substring(0, 23));
        }