TheQuack45

Boolean Adder 2/11/2016

Feb 11th, 2016
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace booleanAdder
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<bool> aList = new List<bool>();
  15.             List<bool> bList = new List<bool>();
  16.             List<bool> sList = new List<bool>();
  17.  
  18.             Console.WriteLine("Write out `a` value (eg 101011): ");
  19.             string aInput = Console.ReadLine();
  20.             List<string> aListString = Regex.Split(aInput, "").ToList<string>();
  21.             aList = parseStringListToBoolList(aListString);
  22.  
  23.             Console.WriteLine("Write out `b` value (eg 101011): ");
  24.             string bInput = Console.ReadLine();
  25.             List<string> bListString = Regex.Split(bInput, "").ToList<string>();
  26.             bList = parseStringListToBoolList(bListString);
  27.  
  28.             multiBitBinAdd(aList, bList, out sList);
  29.  
  30.             Console.WriteLine("Output: ");
  31.             Console.WriteLine(parseBoolToString(sList));
  32.  
  33.             Console.ReadKey();
  34.         }
  35.  
  36.         public static List<bool> parseStringListToBoolList(List<string> xList)
  37.         {
  38.             List<bool> resultList = new List<bool>();
  39.  
  40.             foreach (string x in xList)
  41.             {
  42.                 if (x == "1")
  43.                 {
  44.                     resultList.Add(true);
  45.                 }
  46.                 else if (x == "0")
  47.                 {
  48.                     resultList.Add(false);
  49.                 }
  50.             }
  51.  
  52.             return resultList;
  53.         }
  54.  
  55.         public static string parseBoolToString(List<bool> xList)
  56.         {
  57.             string xString = "";
  58.  
  59.             foreach (bool x in xList)
  60.             {
  61.                 if (x == true)
  62.                     xString += "1";
  63.                 else
  64.                     xString += "0";
  65.             }
  66.  
  67.             return xString;
  68.         }
  69.  
  70.         public static List<bool> padOutList(List<bool> listA, List<bool> listB)
  71.         {
  72.             List<bool> tempList = listB.Reverse<bool>().ToList<bool>();
  73.             for (int i = listB.Count; i < listA.Count; i++)
  74.             {
  75.                 // Add false values to pad out smaller list
  76.                 tempList.Add(false);
  77.             }
  78.  
  79.             // Set listB to reverse of padded tempList
  80.             listB = tempList.Reverse<bool>().ToList<bool>();
  81.  
  82.             return listB;
  83.         }
  84.  
  85.         public static List<bool> reverseList(List<bool> xList)
  86.         {
  87.             List<bool> xListRev = new List<bool>();
  88.             for (int i = xList.Count - 1; i >= 0; i--)
  89.             {
  90.                 xListRev.Add(xList[i]);
  91.             }
  92.  
  93.             return xListRev;
  94.         }
  95.  
  96.         public static void multiBitBinAdd(List<bool> aList, List<bool> bList, out List<bool> sList, bool carryIn = false)
  97.         {
  98.             if (aList.Count > bList.Count)
  99.             {
  100.                 // Lists are not of same length; change size of bList to fit
  101.                 bList = padOutList(aList, bList);
  102.             }
  103.             else if (bList.Count > aList.Count)
  104.             {
  105.                 // Lists are not of same length; change size of aList to fit
  106.                 aList = padOutList(bList, aList);
  107.             }
  108.  
  109.             List<bool> sListRev = new List<bool>();
  110.             sList = new List<bool>();
  111.             bool carryOut = false;
  112.             bool s;
  113.  
  114.             aList = aList.Reverse<bool>().ToList<bool>();
  115.             bList = bList.Reverse<bool>().ToList<bool>();
  116.  
  117.             for (int i = 0; i < aList.Count; i++)
  118.             {
  119.                 binAdd(aList[i], bList[i], carryIn, out s, out carryOut);
  120.  
  121.                 sList.Add(s);
  122.  
  123.                 carryIn = carryOut;
  124.             }
  125.  
  126.             if (carryOut)
  127.                 sList.Add(carryOut);
  128.  
  129.             sList = sList.Reverse<bool>().ToList<bool>();
  130.         }
  131.  
  132.         public static void binAdd(bool a, bool b, bool carryIn, out bool s, out bool carryOut)
  133.         {
  134.             if (a ^ b)
  135.             {
  136.                 // Exclusive OR 1 true
  137.  
  138.                 if (carryIn)
  139.                 {
  140.                     // Exclusive OR 2 false
  141.                     s = false;
  142.  
  143.                     // AND 2 true
  144.                     carryOut = true;
  145.                 }
  146.                 else
  147.                 {
  148.                     // Exclusive OR 2 true
  149.                     s = true;
  150.                     carryOut = false;
  151.                 }
  152.  
  153.                 return;
  154.             }
  155.             else if (a && b)
  156.             {
  157.                 // AND 1 true
  158.                 carryOut = true;
  159.  
  160.                 if (carryIn)
  161.                 {
  162.                     s = true;
  163.                 }
  164.                 else
  165.                 {
  166.                     s = false;
  167.                 }
  168.  
  169.                 return;
  170.             }
  171.             else
  172.             {
  173.                 // Exclusive OR 1 false
  174.                 // AND 1 false
  175.                 if (carryIn)
  176.                 {
  177.                     s = true;
  178.                 }
  179.                 else
  180.                 {
  181.                     s = false;
  182.                 }
  183.  
  184.                 carryOut = false;
  185.  
  186.                 return;
  187.             }
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment