TheQuack45

booleanAdder

Jan 26th, 2016
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace booleanAdder
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool a, b, carryIn, s, carryOut;
  14.  
  15.             List<bool> aList = new List<bool>();
  16.             List<bool> bList = new List<bool>();
  17.  
  18.             /*
  19.             Console.Write("a: ");
  20.             a = boolParseInt(Int32.Parse(Console.ReadLine()));
  21.  
  22.             Console.Write("b: ");
  23.             b = boolParseInt(Int32.Parse(Console.ReadLine()));
  24.  
  25.             Console.Write("carryIn: ");
  26.             carryIn = boolParseInt(Int32.Parse(Console.ReadLine()));
  27.  
  28.             binAdd(a, b, carryIn, out s, out carryOut);
  29.  
  30.             Console.WriteLine("s: " + s.ToString());
  31.             Console.WriteLine("carryOut: " + carryOut.ToString());
  32.             */
  33.  
  34.             string input;
  35.             bool go = true;
  36.             Console.WriteLine("Write out all `a` values (0, 1). Type 'e' to stop.");
  37.             while (go)
  38.             {
  39.                 input = Console.ReadLine();
  40.                 if (input != "e")
  41.                     aList.Add(boolParseInt(Int32.Parse(input)));
  42.                 else
  43.                     go = false;
  44.             }
  45.  
  46.             go = true;
  47.             Console.WriteLine("Write out all `b` values (0, 1). Type 'e' to stop.");
  48.             while (go)
  49.             {
  50.                 input = Console.ReadLine();
  51.                 if (input != "e")
  52.                     bList.Add(boolParseInt(Int32.Parse(input)));
  53.                 else
  54.                     go = false;
  55.             }
  56.  
  57.             List<bool> sList = new List<bool>();
  58.  
  59.             multiBitBinAdd(aList, bList, out sList);
  60.  
  61.             Console.WriteLine("Output: ");
  62.             Console.WriteLine(parseBinary(sList));
  63.  
  64.             Console.ReadKey();
  65.         }
  66.  
  67.         public static bool boolParseInt(int x)
  68.         {
  69.             if (x == 1)
  70.             {
  71.                 return true;
  72.             }
  73.             else
  74.             {
  75.                 return false;
  76.             }
  77.         }
  78.  
  79.         public static int parseBinary(List<bool> xList)
  80.         {
  81.             string xString = "";
  82.  
  83.             foreach (bool x in xList)
  84.             {
  85.                 if (x == true)
  86.                     xString += "1";
  87.                 else
  88.                     xString += "0";
  89.  
  90.             }
  91.  
  92.             return Int32.Parse(xString);
  93.         }
  94.  
  95.         public static void multiBitBinAdd(List<bool> aList, List<bool> bList, out List<bool> sList, bool carryIn = false)
  96.         {
  97.             int aListCount = aList.Count;
  98.             if (aListCount != bList.Count)
  99.             {
  100.                 // Lists are not of same length; fail
  101.                 sList = null;
  102.  
  103.                 return;
  104.             }
  105.  
  106.             sList = new List<bool>();
  107.             bool s;
  108.             bool carryInCheck = false;
  109.  
  110.             for (int i = 0; i < aList.Count; i++)
  111.             {
  112.                 carryInIfExit:
  113.  
  114.                 // Loop through all bits in list
  115.                 binAdd(aList[i], bList[i], carryIn, out s, out carryIn);
  116.  
  117.                 if (carryIn)
  118.                 {
  119.                     sList.Add(true);
  120.                    
  121.                     // This if statement goes to the else clause even if carryInCheck is true
  122.                     // TODO: Wtf?
  123.                     if (carryInCheck)
  124.                     {
  125.                         if (s)
  126.                             sList.Add(true);
  127.                         else
  128.                             carryIn = true;
  129.  
  130.                         carryInCheck = false;
  131.                         goto carryInIfExit;
  132.                     }
  133.  
  134.                     carryInCheck = true;
  135.                 }
  136.                 else
  137.                 {
  138.                     sList.Add(s);
  139.                 }
  140.             }
  141.         }
  142.  
  143.         public static void binAdd(bool a, bool b, bool carryIn, out bool s, out bool carryOut)
  144.         {
  145.             if (a ^ b)
  146.             {
  147.                 // Exclusive OR 1 true
  148.  
  149.                 if (carryIn)
  150.                 {
  151.                     // Exclusive OR 2 false
  152.                     s = false;
  153.  
  154.                     // AND 2 true
  155.                     carryOut = true;
  156.                 }
  157.                 else
  158.                 {
  159.                     // Exclusive OR 2 true
  160.                     s = true;
  161.                     carryOut = false;
  162.                 }
  163.  
  164.                 return;
  165.             }
  166.             else if (a && b)
  167.             {
  168.                 // AND 1 true
  169.                 carryOut = true;
  170.  
  171.                 if (carryIn)
  172.                 {
  173.                     s = true;
  174.                 }
  175.                 else
  176.                 {
  177.                     s = false;
  178.                 }
  179.  
  180.                 return;
  181.             }
  182.             else
  183.             {
  184.                 // Exclusive OR 1 false
  185.                 // AND 1 false
  186.                 if (carryIn)
  187.                 {
  188.                     s = true;
  189.                 }
  190.                 else
  191.                 {
  192.                     s = false;
  193.                 }
  194.  
  195.                 carryOut = false;
  196.  
  197.                 return;
  198.             }
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment