代码拉取完成,页面将自动刷新
using System.Text;
namespace Algorithms.Other;
/// <summary>
/// Manually converts an integer of certain size to a string of the binary representation.
/// </summary>
public static class Int2Binary
{
/// <summary>
/// Returns string of the binary representation of given Int.
/// </summary>
/// <param name="input">Number to be converted.</param>
/// <returns>Binary representation of input.</returns>
public static string Int2Bin(ushort input)
{
ushort msb = ushort.MaxValue / 2 + 1;
var output = new StringBuilder();
for (var i = 0; i < 16; i++)
{
if (input >= msb)
{
output.Append("1");
input -= msb;
msb /= 2;
}
else
{
output.Append("0");
msb /= 2;
}
}
return output.ToString();
}
/// <summary>
/// Returns string of the binary representation of given Int.
/// </summary>
/// <param name="input">Number to be converted.</param>
/// <returns>Binary representation of input.</returns>
public static string Int2Bin(uint input)
{
var msb = uint.MaxValue / 2 + 1;
var output = new StringBuilder();
for (var i = 0; i < 32; i++)
{
if (input >= msb)
{
output.Append("1");
input -= msb;
msb /= 2;
}
else
{
output.Append("0");
msb /= 2;
}
}
return output.ToString();
}
/// <summary>
/// Returns string of the binary representation of given Int.
/// </summary>
/// <param name="input">Number to be converted.</param>
/// <returns>Binary representation of input.</returns>
public static string Int2Bin(ulong input)
{
var msb = ulong.MaxValue / 2 + 1;
var output = new StringBuilder();
for (var i = 0; i < 64; i++)
{
if (input >= msb)
{
output.Append("1");
input -= msb;
msb /= 2;
}
else
{
output.Append("0");
msb /= 2;
}
}
return output.ToString();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。