Java lds0。这对加法位关系有重要影响,很快就会变得清晰
现在的问题是,等式1.2使用“+”运算符,而bitwiseAdd不使用(它只使用“^”、“&”和“这里是另一种乘法方法Java lds0。这对加法位关系有重要影响,很快就会变得清晰,java,bit-manipulation,bitwise-operators,multiplication,addition,Java,Bit Manipulation,Bitwise Operators,Multiplication,Addition,现在的问题是,等式1.2使用“+”运算符,而bitwiseAdd不使用(它只使用“^”、“&”和“这里是另一种乘法方法 /** * Multiplication of binary numbers without using '*' operator * uses bitwise Shifting/Anding * * @param n1 * @param n2 */ public static void multiply(int n1, int n2) { int temp
/**
* Multiplication of binary numbers without using '*' operator
* uses bitwise Shifting/Anding
*
* @param n1
* @param n2
*/
public static void multiply(int n1, int n2) {
int temp, i = 0, result = 0;
while (n2 != 0) {
if ((n2 & 1) == 1) {
temp = n1;
// result += (temp>>=(1/i)); // To do it only using Right shift
result += (temp<<=i); // Left shift (temp * 2^i)
}
n2 >>= 1; // Right shift n2 by 1.
i++;
}
System.out.println(result);
}
/**
*不使用“*”运算符的二进制数乘法
*使用按位移位/和运算
*
*@param n1
*@param n2
*/
公共静态空乘(整数n1,整数n2){
内部温度,i=0,结果=0;
而(n2!=0){
如果((n2&1)==1){
温度=n1;
//结果+=(temp>>=(1/i));//仅使用右移键
结果+=(温度=1;//将n2右移1。
i++;
}
系统输出打印项次(结果);
}
乘法1与你在中学学到的纸笔算法非常相似,只有二进制。它使用了001011*X=001000*X+000010*X+000001*X这一事实。只需一个简单的问题:为什么每次迭代后都要加上n1*2^i?@user183037-除非设置了n2的当前位,否则我们不会加上n1*2^i。但是,我们不会o将a的值放大2倍,以便在下一次迭代开始时,它的值为n1*2^{i+1}。这是否澄清了问题?如果b由于“>>”的性质为负值,这将不起作用运算符。应使用b>>>>=1。值得注意的是,可以做一些事情来大大减少所有按位加法所需的时间。可以轻松地重写一个数字,以便将每对位替换为-2到+2之间的值[+3或-3将由-1或+1连同进位或借位一起处理,从而将所需的加法数量减少一半。更重要的是,a+b+c可以计算为(a^b^c)+((a&b | b&c | a&c)…可以使用上述公式N-2次,将整个输入字符串转换为只需要添加的两个数字。
0 + 0 = 0 # 0 xor 0 = 0
0 + 1 = 1 # 0 xor 1 = 1
1 + 0 = 1 # 1 xor 0 = 1
1 + 1 = 10 # 1 xor 1 = 0 ( read 1 + 1 = 10 as 1 + 1 = 0 and 1 carry)
public static void bitwiseMultiply(int n1, int n2) {
/* This value will hold n1 * 2^i for varying values of i. It will
* start off holding n1 * 2^0 = n1, and after each iteration will
* be updated to hold the next term in the sequence.
*/
int a = n1;
/* This value will be used to read the individual bits out of n2.
* We'll use the shifting trick to read the bits and will maintain
* the invariant that after i iterations, b is equal to n2 >> i.
*/
int b = n2;
/* This value will hold the sum of the terms so far. */
int result = 0;
/* Continuously loop over more and more bits of n2 until we've
* consumed the last of them. Since after i iterations of the
* loop b = n2 >> i, this only reaches zero once we've used up
* all the bits of the original value of n2.
*/
while (b != 0)
{
/* Using the bitwise AND trick, determine whether the ith
* bit of b is a zero or one. If it's a zero, then the
* current term in our sum is zero and we don't do anything.
* Otherwise, then we should add n1 * 2^i.
*/
if ((b & 1) != 0)
{
/* Recall that a = n1 * 2^i at this point, so we're adding
* in the next term in the sum.
*/
result = result + a;
}
/* To maintain that a = n1 * 2^i after i iterations, scale it
* by a factor of two by left shifting one position.
*/
a <<= 1;
/* To maintain that b = n2 >> i after i iterations, shift it
* one spot over.
*/
b >>>= 1;
}
System.out.println(result);
}
x + y = 2 * (x&y)+(x^y) (1.1)
x + y = 2 * and + xor (1.2)
with
and = x & y
xor = x ^ y
x + y = 2*(2*and & xor) + (2*and ^ xor) (1.3)
x + y = 2 * and[1] + xor[1] (1.4)
with
and[1] = 2*and & xor,
xor[1] = 2*and ^ xor,
[1] meaning 'recursed one time'
x + y = 2 * and[2] + xor[2]
x + y = 2 * and[3] + xor[3]
x + y = 2 * and[x] + xor[x] (1.5)
with x the nth recursion
x + y = xor[x] (1.6)
x + y = 2(x&y)+(x^y) (1.1)
x = 9 (1001)
y = 13 (1101)
x + y = 9 + 13 = 22
x & y = 9 & 13 = 9 (1001 & 1101 = 1001)
x ^ y = 9^13 = 4 (1001 ^ 1101 = 0100)
9 + 13 = 2 * 9 + 4 = 22 et voila!
x + y = 2 * and + xor (equation 1.2)
x + y = 2*(2*and & xor) + (2*and ^ xor) (equation 1.3)
x + y = 2(x&y) + (x^y) (equation 1.1)
[2(x&y)] + (x^y) = 2 ([2(x&y)] & (x^y)) + ([2(x&y)] ^ (x^y))
(left side of equation 1.1) (after applying the addition/bitwise operands relationship)
[2(x&y)] + (x^y) = 2*(2*and & xor) + (2*and ^ xor)
with
and = x&y
xor = x^y
2*(2*and & xor) + (2*and ^ xor) = 2*and[1] + xor[1]
with
and[1] = 2*and & xor
xor[1] = 2*and ^ xor
[1] meaning 'recursed one time'
/**
* Multiplication of binary numbers without using '*' operator
* uses bitwise Shifting/Anding
*
* @param n1
* @param n2
*/
public static void multiply(int n1, int n2) {
int temp, i = 0, result = 0;
while (n2 != 0) {
if ((n2 & 1) == 1) {
temp = n1;
// result += (temp>>=(1/i)); // To do it only using Right shift
result += (temp<<=i); // Left shift (temp * 2^i)
}
n2 >>= 1; // Right shift n2 by 1.
i++;
}
System.out.println(result);
}