Ejemplo n.º 1
0
        public static BinaryNumber Invert(BinaryNumber num, int size)
        {
            if (size < num.Length)
            {
                throw new ApplicationException("Wrong size!");
            }
            BinaryNumber result = new BinaryNumber(size);

            for (int i = 0; i < num.Length; i++)
            {
                result[i] = num[i];
            }
            BinaryNumber temp  = HalfInvert(result);
            BinaryNumber temp1 = DecToBin(1);
            BinaryNumber temp2 = temp + temp1;
            int          n     = num.Length;

            return(Truncate(temp2, size));
        }
Ejemplo n.º 2
0
        public static BinaryNumber Truncate(BinaryNumber num)
        {
            int temp = 0;

            for (int i = num.Length; i > 0; i--)
            {
                if (num[i - 1])
                {
                    temp = i;
                    break;
                }
            }
            BinaryNumber result = new BinaryNumber(temp);

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = num[i];
            }
            return(result);
        }
Ejemplo n.º 3
0
        public static BinaryFloat32 operator *(BinaryFloat32 n1, BinaryFloat32 n2)
        {
            BinaryFloat32 result = new BinaryFloat32();

            if (n1.IsZero || n2.IsZero)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = false;
                }
                return(result);
            }
            if (n1.IsInfinity || n2.IsInfinity)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = true;
                }
                return(result);
            }
            //setting sign
            result.Sign = n1.Sign ^ n2.Sign;

            //finding result mantissa
            //-creating mantissa with added hidden 1
            int          needExp = 0;
            BinaryNumber temp_m1 = new BinaryNumber("1" + n1.Mantissa.ToString());
            BinaryNumber temp_m2 = new BinaryNumber("1" + n2.Mantissa.ToString());

            BinaryNumber temp_m = temp_m1 * temp_m2;

            if (temp_m[temp_m.Length - 1])
            {
                needExp = 1;
            }
            //11.0101010101010101010101010101010101010101010101
            BinaryNumber NewM = new BinaryNumber(23);

            for (int i = 45 + needExp; i > 45 + needExp - 23; i--)
            {
                NewM[i - 23 - needExp] = temp_m[i];
            }

            result.Mantissa = NewM;

            ///finding exponent
            BinaryNumber NewExp = Truncate(n1.Exponent + n2.Exponent + DecToBin(needExp) - DecToBin(127));

            if (NewExp.Length > 8 || !NewExp.Contains(false))
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = true;
                }
            }
            else
            {
                result.Exponent = Truncate(NewExp, 8);
            }
            return(result);
        }