Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获得下一个更高的2次方?_C#_Matlab_Ilnumerics - Fatal编程技术网

C# 如何获得下一个更高的2次方?

C# 如何获得下一个更高的2次方?,c#,matlab,ilnumerics,C#,Matlab,Ilnumerics,我想将以下Matlab代码转换为C#: 其中NEXTPOW2(N)表示Matlab中的下一个更高的2次方 那么,我们如何在C#代码中实现相同的功能,或者在ilnumerics Lab的帮助下实现相同的功能呢?如果我正确理解了您的问题: x = 129; NextPow = round(2^ceil(log2(x))) % Gives 256 for x = 129 % Gives 2 for x = 2

我想将以下Matlab代码转换为C#:

其中
NEXTPOW2(N)
表示Matlab中的下一个更高的2次方


那么,我们如何在C#代码中实现相同的功能,或者在ilnumerics Lab的帮助下实现相同的功能呢?

如果我正确理解了您的问题:

x = 129;
NextPow = round(2^ceil(log2(x))) % Gives 256 for x = 129
                                 % Gives 2 for x = 2
                                 % Gives 16 for x = 15

如果我正确理解你的问题:

x = 129;
NextPow = round(2^ceil(log2(x))) % Gives 256 for x = 129
                                 % Gives 2 for x = 2
                                 % Gives 16 for x = 15
可能是最有效的方法,前面也提到过:

可能是最有效的方法,前面也提到过:

在.NETCore中,您可以使用或来获取最重要位的位置,然后

return 1L << (BitOperations.Log2(nn - 1) + 1); // or
return 1L << (63 - BitOperations.LeadingZeroCount(nn));
return 1L在.NET内核中,您可以使用或获取最高有效位的位置,然后

return 1L << (BitOperations.Log2(nn - 1) + 1); // or
return 1L << (63 - BitOperations.LeadingZeroCount(nn));

<代码>返回1L看一看非常好的例子。如果这是对你最好的答案,请考虑接受它。非常好的例子。如果这是对你最好的答案,请考虑接受它。