Given two binary strings, return their sum (also a binary string). For example, a = "1101" // 13 b = "1000" // 8 Return “1101000”. // 104 Solution: implement Binary add to perform multication static int MultipleWithoutOperator(int num, int multiplier) { StringBuilder sb = new StringBuilder(); string binary = Convert.ToString(num, 2); string binaryMultiplier = Convert.ToString(multiplier, 2); string temp = ""; for (int i = binaryMultiplier.Length-1, j=0;i >= 0;i--,j++){ if (binaryMultiplier[i]=='1') { if (string.IsNullOrEmpty(temp)) { temp = binary.PadRight(binary.Length + j, '0'); } else { temp = BinaryAdd(temp, binary.PadRight(binary.Length + j, '0')); } } } return Convert.ToInt32(temp, 2); } static string BinaryAdd(string a, string b) { int carryover = 0; Stri...
留言
張貼留言