Created
June 28, 2016 18:21
-
-
Save jianminchen/5442cb579b13ffc203ce89b3e54e8a36 to your computer and use it in GitHub Desktop.
A|B = C calculation - Hexadecimal - integer - binary string - work on a small well-defined function first.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System | |
; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace AorB_Equal_C | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string result = AorB_Calculation("111A", "1B" ); | |
// test result: 111B | |
} | |
/* | |
* AorB_Calculation - June 28, 2016 | |
* | |
* HexaDecimal value calculation: | |
* A | B | |
* A is in the range of (0, 10^5000) | |
* B is in the range of (0, 10^5000) | |
* | |
* A | B | |
* For example: | |
* A = "111A" | |
* B = "1B" | |
* so, A|B = "111B" | |
* | |
* First practice: | |
* bug001 - line 69, binary string is in reverse order, and then, reverse the string before it is converted to integer. | |
* Also, change string name from binaryStr -> binaryStr_ReverseOrder | |
*/ | |
public static string AorB_Calculation(string s1, string s2) | |
{ | |
if (s1 == null && s2 == null) | |
return ""; | |
if (s1 == null) | |
return s2; | |
if (s2 == null) | |
return s1; | |
s1 = Reverse(s1); | |
s2 = Reverse(s2); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < Math.Max(s1.Length, s2.Length); i++) | |
{ | |
StringBuilder binaryStr_ReverseOrder = new StringBuilder(); | |
int value1 = (i < s1.Length) ? hexaDecimalCharToInt(s1[i]) : 0; | |
int value2 = (i < s2.Length) ? hexaDecimalCharToInt(s2[i]) : 0; | |
while (value1 > 0 || value2 > 0) | |
{ | |
int[] leastSignificantBit = { value1 &1, value2&1 }; | |
int orValue = leastSignificantBit[0] | leastSignificantBit[1] ; | |
binaryStr_ReverseOrder.Append((char)(orValue + '0')); | |
// right shift one bit | |
value1 = value1 >> 1; | |
value2 = value2 >> 1; | |
} | |
//sb.Append(convertBinaryStringToHexaDecimal(binaryStr.ToString())); // bug001 - binaryStr - reverseOrder | |
sb.Append(convertBinaryStringToHexaDecimal(Reverse(binaryStr_ReverseOrder.ToString()))); | |
} | |
return Reverse(sb.ToString()); | |
} | |
/* | |
* input is string of binary number, at most 4 bits, | |
* for example, "1100" -> 12 -> 'C' | |
*/ | |
public static char convertBinaryStringToHexaDecimal(string s) | |
{ | |
int sum = 0; | |
foreach (char c in s) | |
{ | |
int value = (c - '0'); | |
sum = value + sum * 2; | |
} | |
if (sum >= 0 && sum <= 9) | |
return (char)(sum + '0'); | |
else | |
{ | |
string charStr = "ABCDEF"; | |
char[] charArr = charStr.ToCharArray(); | |
return charArr[sum - 10]; | |
} | |
} | |
/* | |
* Convert HexaDecimal char to integer | |
*/ | |
public static int hexaDecimalCharToInt(char c) | |
{ | |
int number = c - '0'; | |
if (number >= 0 && number <= 9) | |
return number; | |
else | |
{ | |
string charStr = "ABCDEF"; | |
char[] charArr = charStr.ToCharArray(); | |
int value = Array.IndexOf(charArr, c); | |
return 10 + value; | |
} | |
} | |
/* | |
* http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string | |
*/ | |
public static string Reverse(string s) | |
{ | |
char[] charArray = s.ToCharArray(); | |
Array.Reverse(charArray); | |
return new string(charArray); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment