Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created June 26, 2016 19:42
Show Gist options
  • Save jianminchen/f4d8761ca6126052e3df792212861436 to your computer and use it in GitHub Desktop.
Save jianminchen/f4d8761ca6126052e3df792212861436 to your computer and use it in GitHub Desktop.
AorB workout example - try to get a well-defined function to work on, and get some practices on bit manipulations.
using System
;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AtoB
{
class Program
{
static void Main(string[] args)
{
string fromValue = "80";
string orValue = "70";
string output = ""; // should be "00"
//int no = updateHexaDecimal_Remove1s(fromValue, orValue, ref output);
int no2 = updateHexaDecimal_Remove1s("F1", "70", ref output); // should be no2 = 2, output = "70"
}
/*
* HexaDecimal Char update
* For example, A = 1000
* A | B = C,
* C = 0111
* Then, 4th bit 1 in A should be set to 1.
* number is 1, A' = 0000
*
* Another example, A = 1100,
* A | B = C,
* C = 0111,
* then, 4th bit 1 in A should be set to 0
* function return is 1, A' = 0100
*
* 1. count how many bits to update (only bit with value 1)
* 2. return A's new value A'
* 3. Work on bits from left to right, lowest significant bit first
* 4. assume the value > 0, and value < 10^ 50000
*
* Work on HexaDecimal string,
* For example, D8, work on '8' first, 4 bits 1000, from right to left;
* and then, work on 'D', 1101 - 13
*
* edge case:
* from = "8D",
* orValue = "E",
* orValue is short in length.
*
* This function design is questionable - need to work on later, make it as simple as possible
*
*/
public static int updateHexaDecimal_Remove1s(string from, string orValue, ref string output)
{
int count = 0;
StringBuilder sb_Out = new StringBuilder();
for (int i = from.Length - 1; i >= 0; i--)
{
int nFrom = hexaDecimalCharToInt(from[i]);
int nOrValue = (i < orValue.Length) ? hexaDecimalCharToInt(orValue[i]) : 0;
string s1 = "";
count += getBinaryStrInReverseOrder(nFrom, nOrValue, ref s1);
sb_Out.Append(convertBinaryStringToHexaDecimal(Reverse(s1)));
}
output = Reverse(sb_Out.ToString()); // bug001 - need to reverse the binary string
return count;
}
/*
* Make it well defined function -
* work on more!
*
*/
public static int getBinaryStrInReverseOrder(int value, int targetValue, ref string output)
{
if (value == 0)
{
output = "0";
return 0;
}
StringBuilder sb = new StringBuilder();
int count = 0;
while (value > 0)
{
int bitNo = value & 1;
int bitTarget = targetValue & 1;
if ((bitNo == 1 && bitTarget == 0))
{
count++;
bitNo = 0;
}
char c = (char)(bitNo + '0');
sb.Append(c);
// right shift 1 bit
value = value >> 1;
targetValue = targetValue >> 1;
}
output = sb.ToString();
return count;
}
/*
* 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