我正在解决一个我能够解决的问题,除了最后一块之外 - 我不知道如何使用按位运算符进行乘法运算:
0*8 = 0
1*8 = 8
2*8 = 16
3*8 = 24
4*8 = 32
Run Code Online (Sandbox Code Playgroud)
能否请您推荐一种方法来解决这个问题?
是否可以将两个数字相乘而不使用算术运算符?使用左移运算符,我可以将任意数乘以2.其他数字怎么样?
我想在.NET中实现两个整数的乘法而不使用乘法运算符
public uint MultiplyNumbers(uint x, uint y)
{
}
Run Code Online (Sandbox Code Playgroud)
任何的想法!
所以我有两个数组列表代表两个数字.这是我不必使用BigInt.所以例如
ArrayList<Integer> LargeInt = [2,3,6,4] would really equal 2,364
ArrayList<Integer> LargeInt2 = [8,7,9,4,6] would be 87,946
Run Code Online (Sandbox Code Playgroud)
我的目标是找出一种方法来将两个数字相乘并对字符串进行回答.我知道在放入一个字符串之前需要将两个数字相乘才能放入另一个数组中,因此它不会因较大的数字而崩溃.我也知道这将是一个循环放入另一个.但我发现很难制作一个能够将这两个数字相乘的代码.乘以的两个数组可以是任意数.
我在其中一个网站上看到了C中的一个面试问题,你被要求编写一个函数,该函数得到2个整数,num和times,并且不使用*运算符多个,这意味着主要使用左右移位.我提出了一个有效的答案(除非有人发现了一个错误),但是有没有人有更好的方法在更好的时间或内存消耗中解决它?
这是我写的:
#include <stdio.h>
int multiply_with_shift (int num, int times)
{
int cnt=0;
int org_times=times;
if((num & times)==0)
return 0;
else
{
while(times >1)
{
times= times >> 1;
cnt++;
}
int val= 1;
val= val <<cnt;
int sub= org_times-val;
int res= num << cnt;
for( int i=0 ; i < sub; i++)
{
res+=num;
}
return res;
}
}
void main()
{
int tmp;
tmp=multiply_with_shift(5,15);
printf(" the answer is : %d \n", tmp);
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
?