/// <summary> /// Where bit traker 'checker' will store/set bit for each charecter /// and when upon 1&1 it indiacate that charecter already exists. /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsUniqueChars_UsingBitTracker(string str) { string prefix = "IsUniqueChars_UsingBitTracker: "; Console.WriteLine($"{prefix} str={str}"); if (str.Length > 256) { return(false); } var checker = 0; for (var i = 0; i < str.Length; i++) { var val = str[i] - 'a'; Console.WriteLine($"{prefix} str[i]=" + str[i]); //Console.WriteLine($"{prefix} val= (str[i] - 'a') = {str[i] - 'a'} =" + val); Console.WriteLine($"{prefix} checker= {checker} [{AssortedMethods.ToFullBinarystring(checker)}]"); Console.WriteLine($"{prefix} (1 << val)=(1 [{AssortedMethods.ToFullBinarystring(1)}] << {val}) = {(1 << val)} [{AssortedMethods.ToFullBinarystring((1 << val))}]"); Console.WriteLine($"{prefix} (checker & (1 << val)) > 0 = [{AssortedMethods.ToFullBinarystring(checker)}] & [{AssortedMethods.ToFullBinarystring((1 << val))}] = [{AssortedMethods.ToFullBinarystring((checker & (1 << val)))}] > 0 = " + ((checker & (1 << val)) > 0)); if ((checker & (1 << val)) > 0) { Console.WriteLine($"{prefix} Return False"); return(false); } Console.WriteLine($"{prefix} checker |= (1 << val) = [{AssortedMethods.ToFullBinarystring(checker | (1 << val))}]" + (checker | (1 << val))); checker |= (1 << val); } return(true); }
public void Run() { var a = 103217; Console.WriteLine(a + ": " + AssortedMethods.ToFullBinarystring(a)); var b = SwapOddEvenBits(a); Console.WriteLine(b + ": " + AssortedMethods.ToFullBinarystring(b)); }
public int ClearBit(int number, int i) { int mask = ~(1 << i); Console.WriteLine($"ClearBit:(number({number})={AssortedMethods.ToFullBinarystring(number, 4)}) " + $"& mask({mask})={AssortedMethods.ToFullBinarystring(mask, 4)})={(number & mask)} " + $"mask= ~(1 << {i})=~[{AssortedMethods.ToFullBinarystring(1 << i, 4)}] =[{AssortedMethods.ToFullBinarystring(~(1 << i), 4)}]={mask}"); return(number & mask); }
/// <summary> /// clear all right to ith position /// </summary> /// <param name="number"></param> /// <param name="i"></param> /// <returns></returns> public int ClearBitsMSBthroughtI_RightRemova(int number, int i) { int mask = (1 << i) - 1; Console.WriteLine($"ClearBitsMSBthroughtI:(number({number})={AssortedMethods.ToFullBinarystring(number, 4)}) " + $"& mask({mask})={AssortedMethods.ToFullBinarystring(mask, 4)})={(number & mask)} " + $"mask= ((1 << i) - 1)=(1 << i) [{AssortedMethods.ToFullBinarystring(1 << i, 4)}]-1 [{AssortedMethods.ToFullBinarystring(-1, 4)}]=[{AssortedMethods.ToFullBinarystring((1 << i) - 1, 4)}]={mask}" + $""); return(number & mask); }
/// <summary> /// clear all left to ith position /// </summary> /// <param name="number"></param> /// <param name="i"></param> /// <returns></returns> public int ClearBitIthrought0_LeftRemoval(int number, int i) { int mask = -1 << (i + 1); Console.WriteLine($"ClearBitIthrought0:(number({number})={AssortedMethods.ToFullBinarystring(number, 4)}) " + $"& mask({mask})={AssortedMethods.ToFullBinarystring(mask, 4)})={(number & mask)} " + $"mask= ( -1 << (i + 1))={mask}" + $"mask= (-1 << (i + 1))=-1 [{AssortedMethods.ToFullBinarystring(-1, 4)}] << (i + 1) [{AssortedMethods.ToFullBinarystring(i + 1, 4)}]=[{AssortedMethods.ToFullBinarystring(-1 << (i + 1), 4)}]={mask}" + $""); return(number & mask); }
public void Run() { var a = 23432; var b = 512132; Console.WriteLine(a + ": " + AssortedMethods.ToFullBinarystring(a)); Console.WriteLine(b + ": " + AssortedMethods.ToFullBinarystring(b)); var nbits = BitSwapRequired(a, b); var nbits2 = BitSwapRequired2(a, b); Console.WriteLine("Required number of bits: " + nbits + " " + nbits2); }
public int UpdateBit(int number, int i, bool bitValue) { int value = bitValue ? 1 : 0; int mask = ~(1 << i); Console.WriteLine($"ClearBit:(number({number})={AssortedMethods.ToFullBinarystring(number, 4)}) " + $"& mask({mask})={AssortedMethods.ToFullBinarystring(mask, 4)})={(number & mask)} " + $"mask= ~(1 << {i})=~[{AssortedMethods.ToFullBinarystring(1 << i, 4)}] =[{AssortedMethods.ToFullBinarystring(~(1 << i), 4)}]={mask}"); Console.WriteLine($"UpdateBit: ClearBit(number, i)[{AssortedMethods.ToFullBinarystring(ClearBit(number, i), 4)}] | (value[[{AssortedMethods.ToFullBinarystring(value, 4)}]] << 1) = [{AssortedMethods.ToFullBinarystring(value << 1, 4)}]"); //return (ClearBit(number, i) | (value << 1)); return((number & mask) | (value << 1)); }
/* Create a random array of numbers from 0 to n, but skip 'missing' */ public static List <BitInteger> Initialize(int n, int missing) { BitInteger.IntegerSize = AssortedMethods.ToFullBinarystring(n).Length; var array = new List <BitInteger>(); for (var i = 1; i <= n; i++) { array.Add(new BitInteger(i == missing ? 0 : i)); } var random = new Random(); // Shuffle the array once. for (var i = 0; i < n; i++) { var rand = i + (int)(random.Next(1) * (n - i)); array[i].SwapValues(array[rand]); } return(array); }
private static System.Collections.ArrayList GetAlternatingSequnces(int n) { ArrayList sequences = new ArrayList(); int searchingFor = 0; int counter = 0; for (int i = 0; i < 4 * 8; i++) // Interger.Byte*8 { if ((n & 1) != searchingFor) { sequences.Add(counter); searchingFor = n & 1;//Flip 1 to 0 and 0 to 1 counter = 0; } counter++; var nBefore = AssortedMethods.ToFullBinarystring(n); var nAfter = AssortedMethods.ToFullBinarystring((n >> 1)); n = (n >> 1); } return(sequences); //throw new NotImplementedException(); }
public static void BinPrint(int number) { Console.WriteLine(number + ": " + AssortedMethods.ToFullBinarystring(number)); }