public static string ConvertDecimalToBinary(int decimalNumber) { bool isNegative = decimalNumber < 0; decimalNumber = Math.Abs(decimalNumber); StringBuilder sb = new StringBuilder(); // decimal to binary: // 5 = 5 / 2 = 2 reminder 1; // 2 / 2 = 1 reminder 0; // 1 / 2 = 0 reminder 1 = 101 (reversed); while (decimalNumber != 0) { sb.Append(decimalNumber % 2); decimalNumber = decimalNumber / 2; } string result = ExtensionMethods.ReverseString(sb.ToString()); if (isNegative) { result = ExtensionMethods.InvertBinaryNumber(result, NumberOfBits); result = ExtensionMethods.AddBinaryNumbers(result, "1", NumberOfBits); } return(result); }