c# - 翻译 C unsigned long long and bitwise in for loop to c#

标签 c# c

int i, k;
unsigned long long x, y;
k = 5;
for (x = 0; x < 1ULL<<(2*k); ++x) {
    for (i = 0, y = x; i < k; ++i, y >>= 2)
        putchar("ACGT"[y&3]);
    putchar('\n');
}

如何翻译 for (x = 0; x < 1ULL<<(2*k); ++x) unsigned long long 1ULL 和按位到 c#?

我在努力

    public static ulong aux(int val){
        ulong result = ????? << val;
        return result;
    }
    public static void main() {
        int i;
        int k =5;  
        ulong  x,y;
        for (x = 0; x < aux(2*k) ; ++x) {
            for (i = 0, y = x; i < k; ++i, y >>= 2){
                Console.Write("ACGT"[y&3]);
            }
            Console.WriteLine();
    }

如何翻译那部分代码 x < 1ULL<<(2*k);"ACGT"[y&3]

最佳答案

这两行在 C# 中的表达方式非常相似。 C线:

 for (x = 0; x < 1ULL<<(2*k); ++x) {

用 C# 翻译成

 for (x = 0; x < 1UL<<(2*k); ++x) {

(注意后缀 1UL(unsigned long)而不是 1ULL(unsigned long long)。

C线

putchar("ACGT"[y&3]);

需要在 C# 中转换并转换为

Console.Write("ACGT"[(int)y & 3]);

强制转换是必需的,因为索引器 String.Chars , 只能接受有符号整数作为参数。

关于c# - 翻译 C unsigned long long and bitwise in for loop to c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20315365/

相关文章:

java - 如何通过tcp/ip从java接收文件到C?

c# - 用于检查对象层次结构中空值的建议解决方案

c# - WCF 作为 MVC Controller 。模型和 View ?

c# - 如何以编程方式配置MSDTC设置?

c - c中的这两个声明有什么区别?

c - 构建 Wireshark 时的错误和警告

c# - 如何在 Visual Studio 中使用 LINQPad Dump() 扩展方法?

c# - 是否可以禁用 ASP.NET Core 中的数据保护加密?

python - 在 2 种语言之间交换数据

c -/lib/x86_64-linux-gnu/libthread_db.so.1 文件不存在