Binary 二进制re的二进制表示
有什么有效的方法可以找到给定数n的二进制表示的二进制表示吗?Binary 二进制re的二进制表示,binary,Binary,有什么有效的方法可以找到给定数n的二进制表示的二进制表示吗? 其中1这是我的头顶,应该足够快的大多数目的。工程价值不超过1048575 using System; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { checked { uint i = 0;
其中1这是我的头顶,应该足够快的大多数目的。工程价值不超过1048575
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
checked
{
uint i = 0;
while (true)
{
try
{
BinaryOfDecimalRepresentation(++i);
}
catch { Console.WriteLine("Works until " + i); break; }
}
while (true)
{
uint input = 0;
try
{
input = uint.Parse(Console.ReadLine());
}
catch { }
Console.WriteLine(
BinaryOfDecimalRepresentation(input) + " : " +
UInt64ToString(BinaryOfDecimalRepresentation(input)));
}
}
}
static ulong BinaryOfDecimalRepresentation(uint input)
{
checked
{
ulong result = 0;
for (int i = 0; i < 32; i++)
if ((input & 1 << i) != 0) result += (ulong)Math.Pow(10, i);
return result;
}
}
static char[] buffer = new char[64];
static string UInt64ToString(ulong input, bool trim = true)
{
for (int i = 0; i < 64; i++)
buffer[63 - i] = ((input & (ulong)1 << i) != 0) ? '1' : '0';
int firstOne = 0;
if (trim) while (buffer[firstOne] == '0') firstOne++;
return new string(buffer, firstOne, 64 - firstOne);
}
}
}
使用系统;
命名空间控制台应用程序5
{
班级计划
{
静态void Main(字符串[]参数)
{
选中的
{
uint i=0;
while(true)
{
尝试
{
二进制离散表示(++i);
}
catch{Console.WriteLine(“工作到”+i);break;}
}
while(true)
{
uint输入=0;
尝试
{
input=uint.Parse(Console.ReadLine());
}
捕获{}
控制台写入线(
二进制OfDecimalRepresentation(输入)+“:”+
UInt64ToString(二进制的decimalrepresentation(输入));
}
}
}
静态ulong二进制数据表示(uint输入)
{
选中的
{
ulong结果=0;
对于(int i=0;i<32;i++)
如果((input&1)您的意思是“二进制表示(一个数n的二进制表示被重新解释为一个基数为10的数)”是的,那正是我所需要的。这不会导致C/C++中的溢出吗?因为pow(10,i)使用的值大于界限。@AnkurMishra这是C#(没有指定语言)并且不会出现溢出(因此是选中的算术)。请自行验证函数-我不会提供任何保证。我只是将其改进为100万。实际上,我应该在C/C++中使用此功能。因此,请您稍微解释一下算法,它会找出是否设置了一个位,然后将该位的十进制值添加到结果中。例如,如果设置了第二个位,它就知道没有o添加10。您应该能够找出其余部分,并在不需要我帮助的情况下在C/C++中实现它。