今天我需要一个简单的算法来检查一个数是否是2的幂.
算法需要是:
ulong价值.我想出了这个简单的算法:
private bool IsPowerOfTwo(ulong number)
{
if (number == 0)
return false;
for (ulong power = 1; power > 0; power = power << 1)
{
// This for loop used shifting for powers of 2, meaning
// that the value will become 0 after the last shift
// (from binary 1000...0000 to 0000...0000) then, the 'for'
// loop will break out.
if (power == number)
return true;
if (power > number)
return false; …Run Code Online (Sandbox Code Playgroud) 当我正在学习C时,我只是经历了一些基本的东西.我遇到了一个问题,即在不使用*运算符的情况下将数字乘以7.基本上就是这样的
(x << 3) - x;
Run Code Online (Sandbox Code Playgroud)
现在我知道基本的位操作操作,但我不知道如何在不使用*运算符的情况下将数字乘以任何其他奇数?这是一般的算法吗?
Visual Studio或Visual C++ Express的现代(2008/2010)版本是否会在编译代码中生成x86 MUL指令(无符号乘法)?我似乎无法找到或设想它们出现在编译代码中的示例,即使使用无符号类型也是如此.
如果VS不使用MUL进行编译,是否有理由说明原因?
我尝试在网上挖掘以解答我的问题.我找到了一些与达芬奇项目有关的文件.这被标记为JSR 292,它与在JVM中包含闭包有关.这个项目是否实现了,它是Java 8的一部分吗?
我有使用乘法和加法的方法,但我只是无法理解它们.它们都来自外部网站而不是我自己的网站:
public static void bitwiseMultiply(int n1, int n2) {
int a = n1, b = n2, result=0;
while (b != 0) // Iterate the loop till b==0
{
if ((b & 01) != 0) // Logical ANDing of the value of b with 01
{
result = result + a; // Update the result with the new value of a.
}
a <<= 1; // Left shifting the value contained in 'a' by 1.
b >>= 1; // Right shifting …Run Code Online (Sandbox Code Playgroud) java bit-manipulation multiplication addition bitwise-operators
我想知道是否有办法将BigInteger变量相乘,因为*运算符无法应用BigInteger.
所以我想知道是否可以在BigIntegers不使用*运算符的情况下将两个相乘.