private int FindPower(IEEE754 number) { //calculate POWER double power = 0; for (int i = number.Exponent.Length - 1; i >= 0; i--) { power += number.Exponent[i] == 1 ? Math.Pow(2f, number.Exponent.Length - 1 - i) : 0; } return((int)power); }
static void Main(string[] args) { IEEE754 first = new IEEE754("-20"); Console.WriteLine("-20 {0}", first.ToString()); IEEE754 second = new IEEE754("145"); Console.WriteLine("145 {0}", second.ToString()); IEEE754 result = new IEEE754("125"); Console.WriteLine("125 {0}", result.ToString()); Console.WriteLine("START ADDITION"); first.Add(first, second); Console.WriteLine("================================================"); first = new IEEE754("50"); Console.WriteLine("50 {0}", first.ToString()); second = new IEEE754("73"); Console.WriteLine("73 {0}", second.ToString()); result = new IEEE754("123"); Console.WriteLine("123 {0}", result.ToString()); Console.WriteLine("START ADDITION"); first.Add(first, second); Console.WriteLine("================================================"); BinaryHelper helper = new BinaryHelper(); Console.WriteLine("Division as is:"); int firstNumber = int.Parse(Console.ReadLine()); int secondNumber = int.Parse(Console.ReadLine()); var res = helper.Devision(firstNumber, secondNumber); Console.WriteLine("Multiplication shift right"); firstNumber = int.Parse(Console.ReadLine()); secondNumber = int.Parse(Console.ReadLine()); res = helper.Multiply(helper.ConvertToBinary(firstNumber), helper.ConvertToBinary(secondNumber)); string asss = helper.ToScreen(res); Console.WriteLine($"Binary format (help code) {asss}"); Console.ReadKey(); }
public IEEE754 Add(IEEE754 first, IEEE754 second) { var result = new IEEE754(); //first power int firstPower = FindPower(first); int secondPower = FindPower(second); Console.WriteLine($"First Power { firstPower.ToString()}"); Console.WriteLine($"Second Power { secondPower.ToString()}"); //abs (a) > abs (b) int diff = 0; if (firstPower >= secondPower) { diff = firstPower - secondPower; Console.WriteLine($"Abs(A) > Abs(B)"); } else { Console.WriteLine($"Abs(A) < Abs(B) , swapping"); //swapping IEEE754 temp = new IEEE754(); temp.Exponent = first.Exponent; temp.Sign = first.Sign; temp.Normalized = first.Normalized; temp.Fraction = first.Fraction; first.Exponent = second.Exponent; first.Sign = second.Sign; first.Normalized = second.Normalized; first.Fraction = second.Fraction; second.Exponent = temp.Exponent; second.Sign = temp.Sign; second.Normalized = temp.Normalized; second.Fraction = temp.Fraction; int tmp = secondPower; secondPower = firstPower; firstPower = tmp; diff = firstPower - secondPower; } Console.WriteLine($"Diff Power {diff.ToString()}"); int resultPower = firstPower; result.Exponent = first.Exponent; //shift left mantisa of x2 for (int i = 0; i < diff; i++) { if (i == 0) { //shift with normalized bit. second.Fraction = ShiftWithNormalized(second.Fraction); second.Normalized = 0; } else { second.Fraction = Shift(second.Fraction); } } Console.WriteLine($"Shifted Mantisa of Second {second.ToString()}"); if (first.Sign == second.Sign) { Console.WriteLine("Signs are equal"); //add //diff int[] extFrFirst = new int[24]; for (int i = 0; i < first.Fraction.Length; i++) { extFrFirst[i + 1] = first.Fraction[i]; } extFrFirst[0] = first.Normalized; int[] extFrSecond = new int[24]; for (int i = 0; i < second.Fraction.Length; i++) { extFrSecond[i + 1] = second.Fraction[i]; } extFrSecond[0] = second.Normalized; var res = Add(extFrFirst, extFrSecond); int tmp = res.Item2; if (tmp == 1) { result.Fraction = res.Item1; Console.WriteLine($"Unnormalized {result.ToString()}"); int[] extFr = Shift(res.Item1); result.Fraction = new int[23]; result.Normalized = 1; for (int i = 1; i < extFr.Length; i++) { result.Fraction[i - 1] = extFr[i]; } firstPower += 1; result.Exponent = Add(result.Exponent, new int[8] { 0, 0, 0, 0, 0, 0, 0, 1 }).Item1; } else { result.Fraction = new int[23]; result.Normalized = res.Item1[0]; for (int i = 1; i < res.Item1.Length; i++) { result.Fraction[i - 1] = res.Item1[i]; } } } else { Console.WriteLine("Signs aren't equal"); //diff int[] extFrFirst = new int[24]; for (int i = 0; i < first.Fraction.Length; i++) { extFrFirst[i + 1] = first.Fraction[i]; } extFrFirst[0] = first.Normalized; int[] extFrSecond = new int[24]; for (int i = 0; i < second.Fraction.Length; i++) { extFrSecond[i + 1] = second.Fraction[i]; } extFrSecond[0] = second.Normalized; //make second negative var div = extFrSecond.Select((x) => { if (x == 1) { return(0); } else { return(1); } }).ToArray(); var negativeSecond = Add(div, new int[24] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }); var res = Add(extFrFirst, negativeSecond.Item1); if (res.Item1[0] == 0) { //while not zero - normalize int[] newFracton = res.Item1; result.Fraction = newFracton; Console.WriteLine($"Unnormalized {result.ToString()}"); while (newFracton[0] == 0) { newFracton = ShiftLeftWithZero(newFracton); result.Exponent = Add(result.Exponent, new int[24] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }).Item1; } result.Fraction = new int[23]; result.Normalized = 1; for (int i = 1; i < newFracton.Length; i++) { result.Fraction[i - 1] = newFracton[i]; } } else { result.Fraction = new int[23]; result.Normalized = 1; for (int i = 1; i < res.Item1.Length; i++) { result.Fraction[i - 1] = res.Item1[i]; } } } Console.WriteLine($"Normalized Result {result.ToString()}"); return(result); }