Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace booleanAdder
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<bool> aList = new List<bool>();
- List<bool> bList = new List<bool>();
- List<bool> sList = new List<bool>();
- Console.WriteLine("Write out `a` value (eg 101011): ");
- string aInput = Console.ReadLine();
- List<string> aListString = Regex.Split(aInput, "").ToList<string>();
- aList = parseStringListToBoolList(aListString);
- Console.WriteLine("Write out `b` value (eg 101011): ");
- string bInput = Console.ReadLine();
- List<string> bListString = Regex.Split(bInput, "").ToList<string>();
- bList = parseStringListToBoolList(bListString);
- multiBitBinAdd(aList, bList, out sList);
- Console.WriteLine("Output: ");
- Console.WriteLine(parseBoolToString(sList));
- Console.ReadKey();
- }
- public static List<bool> parseStringListToBoolList(List<string> xList)
- {
- List<bool> resultList = new List<bool>();
- foreach (string x in xList)
- {
- if (x == "1")
- {
- resultList.Add(true);
- }
- else if (x == "0")
- {
- resultList.Add(false);
- }
- }
- return resultList;
- }
- public static string parseBoolToString(List<bool> xList)
- {
- string xString = "";
- foreach (bool x in xList)
- {
- if (x == true)
- xString += "1";
- else
- xString += "0";
- }
- return xString;
- }
- public static List<bool> padOutList(List<bool> listA, List<bool> listB)
- {
- List<bool> tempList = listB.Reverse<bool>().ToList<bool>();
- for (int i = listB.Count; i < listA.Count; i++)
- {
- // Add false values to pad out smaller list
- tempList.Add(false);
- }
- // Set listB to reverse of padded tempList
- listB = tempList.Reverse<bool>().ToList<bool>();
- return listB;
- }
- public static List<bool> reverseList(List<bool> xList)
- {
- List<bool> xListRev = new List<bool>();
- for (int i = xList.Count - 1; i >= 0; i--)
- {
- xListRev.Add(xList[i]);
- }
- return xListRev;
- }
- public static void multiBitBinAdd(List<bool> aList, List<bool> bList, out List<bool> sList, bool carryIn = false)
- {
- if (aList.Count > bList.Count)
- {
- // Lists are not of same length; change size of bList to fit
- bList = padOutList(aList, bList);
- }
- else if (bList.Count > aList.Count)
- {
- // Lists are not of same length; change size of aList to fit
- aList = padOutList(bList, aList);
- }
- List<bool> sListRev = new List<bool>();
- sList = new List<bool>();
- bool carryOut = false;
- bool s;
- aList = aList.Reverse<bool>().ToList<bool>();
- bList = bList.Reverse<bool>().ToList<bool>();
- for (int i = 0; i < aList.Count; i++)
- {
- binAdd(aList[i], bList[i], carryIn, out s, out carryOut);
- sList.Add(s);
- carryIn = carryOut;
- }
- if (carryOut)
- sList.Add(carryOut);
- sList = sList.Reverse<bool>().ToList<bool>();
- }
- public static void binAdd(bool a, bool b, bool carryIn, out bool s, out bool carryOut)
- {
- if (a ^ b)
- {
- // Exclusive OR 1 true
- if (carryIn)
- {
- // Exclusive OR 2 false
- s = false;
- // AND 2 true
- carryOut = true;
- }
- else
- {
- // Exclusive OR 2 true
- s = true;
- carryOut = false;
- }
- return;
- }
- else if (a && b)
- {
- // AND 1 true
- carryOut = true;
- if (carryIn)
- {
- s = true;
- }
- else
- {
- s = false;
- }
- return;
- }
- else
- {
- // Exclusive OR 1 false
- // AND 1 false
- if (carryIn)
- {
- s = true;
- }
- else
- {
- s = false;
- }
- carryOut = false;
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment