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.Threading.Tasks;
- namespace booleanAdder
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool a, b, carryIn, s, carryOut;
- List<bool> aList = new List<bool>();
- List<bool> bList = new List<bool>();
- /*
- Console.Write("a: ");
- a = boolParseInt(Int32.Parse(Console.ReadLine()));
- Console.Write("b: ");
- b = boolParseInt(Int32.Parse(Console.ReadLine()));
- Console.Write("carryIn: ");
- carryIn = boolParseInt(Int32.Parse(Console.ReadLine()));
- binAdd(a, b, carryIn, out s, out carryOut);
- Console.WriteLine("s: " + s.ToString());
- Console.WriteLine("carryOut: " + carryOut.ToString());
- */
- string input;
- bool go = true;
- Console.WriteLine("Write out all `a` values (0, 1). Type 'e' to stop.");
- while (go)
- {
- input = Console.ReadLine();
- if (input != "e")
- aList.Add(boolParseInt(Int32.Parse(input)));
- else
- go = false;
- }
- go = true;
- Console.WriteLine("Write out all `b` values (0, 1). Type 'e' to stop.");
- while (go)
- {
- input = Console.ReadLine();
- if (input != "e")
- bList.Add(boolParseInt(Int32.Parse(input)));
- else
- go = false;
- }
- List<bool> sList = new List<bool>();
- multiBitBinAdd(aList, bList, out sList);
- Console.WriteLine("Output: ");
- Console.WriteLine(parseBinary(sList));
- Console.ReadKey();
- }
- public static bool boolParseInt(int x)
- {
- if (x == 1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static int parseBinary(List<bool> xList)
- {
- string xString = "";
- foreach (bool x in xList)
- {
- if (x == true)
- xString += "1";
- else
- xString += "0";
- }
- return Int32.Parse(xString);
- }
- public static void multiBitBinAdd(List<bool> aList, List<bool> bList, out List<bool> sList, bool carryIn = false)
- {
- int aListCount = aList.Count;
- if (aListCount != bList.Count)
- {
- // Lists are not of same length; fail
- sList = null;
- return;
- }
- sList = new List<bool>();
- bool s;
- bool carryInCheck = false;
- for (int i = 0; i < aList.Count; i++)
- {
- carryInIfExit:
- // Loop through all bits in list
- binAdd(aList[i], bList[i], carryIn, out s, out carryIn);
- if (carryIn)
- {
- sList.Add(true);
- // This if statement goes to the else clause even if carryInCheck is true
- // TODO: Wtf?
- if (carryInCheck)
- {
- if (s)
- sList.Add(true);
- else
- carryIn = true;
- carryInCheck = false;
- goto carryInIfExit;
- }
- carryInCheck = true;
- }
- else
- {
- sList.Add(s);
- }
- }
- }
- 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