gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:02:21 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com