110,538
社区成员
发帖
与我相关
我的任务
分享
const uint Baseint = ((uint)1) << 31;
static string Bin(uint num)
{
var valueList = new List<string>();
uint b = Baseint;
while (num > 0)
{
if (num >= b)
{
valueList.Add(b.ToString());
num = num - b;
}
b >>= 1;
if (b <= 0) break;
}
return string.Join(" + ", valueList.ToArray());
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
foreach (uint x in new uint[]{7, 1234567890, uint.MaxValue})
Console.WriteLine("{0} = {1}", x, Bin(x));
}
static string Bin(uint x)
{
uint[] mask =
{
0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x00000100, 0x00000200, 0x00000400, 0x00000800,
0x00001000, 0x00002000, 0x00004000, 0x00008000,
0x00010000, 0x00020000, 0x00040000, 0x00080000,
0x00100000, 0x00200000, 0x00400000, 0x00800000,
0x01000000, 0x02000000, 0x04000000, 0x08000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000,
};
List<string> list = new List<string>();
foreach (uint m in mask)
{
if ((x & m) != 0)
list.Add(m.ToString());
}
return string.Join(" + ", list.ToArray());
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n = 1;
while (n != 0)
{
n = Convert.ToInt32(Console.ReadLine());
ConvertNum(n);
}
}
public static void ConvertNum(int n)
{
Console.WriteLine("n = {0}", n);
Queue<int> s = new Queue<int>();
while (n > 0)
{
s.Enqueue(n % 2);
n /= 2;
}
bool has1 = false;
int i = 0;
while (s.Count > 0)
{
if (s.Dequeue() == 1)
{
has1 = true;
Console.WriteLine(Math.Pow(2, i));
}
i++;
}
if (!has1)
Console.WriteLine(Math.Pow(2, i));
Console.WriteLine();
}
}
}
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ConvertNum(23);
}
public static void ConvertNum(int n)
{
Queue<int> s = new Queue<int>();
while (n > 0)
{
s.Enqueue(n % 2);
n /= 2;
}
bool has1 = false;
int i = 0;
while (s.Count > 0)
{
if (s.Dequeue() == 1)
{
has1 = true;
Console.WriteLine(Math.Pow(2, i));
}
i++;
}
if (!has1)
Console.WriteLine(Math.Pow(2, i));
}
}
}
string x = Convert.ToString(long.Parse(Console.ReadLine()), 2);
int b =1;
for (int i=0; i < x.Length; i++)
{
if (x.Substring(x.Length-i-1,1) == "1")
{
Console.WriteLine("选中"+b);
}
b=b*2;
}
Console.ReadLine();