是否有可能通过使用纯位加法,减法除以10的无符号整数,也许繁衍?使用资源非常有限且速度慢的处理器.
int x = n / 3; // <-- make this faster
// for instance
int a = n * 3; // <-- normal integer multiplication
int b = (n << 1) + n; // <-- potentially faster multiplication
Run Code Online (Sandbox Code Playgroud) 上周我接受了采访,有一个这样的测试:
使用SHIFT LEFT,SHIFT RIGHT,ADD,SUBSTRACT指令计算N/9(给定N为正整数)
.
我是自学C++和Bjarne Stroustrup的"Programming-Principles and Practices Using C++"一书.其中一个"试试这个"问:
在不使用乘法运算符的情况下实现square(); 也就是说,通过重复添加来执行x*x(在0处开始变量结果并将x添加到x次).然后使用该square()运行某个版本的"第一个程序".
基本上,我需要创建一个square(int x)函数,它将返回它的平方而不使用乘法运算符.到目前为止我有这个:
int square(int x)
{
int i = 0;
for(int counter = 0; counter < x; ++counter)
{
i = i + x;
}
return i;
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更好的方法来做到这一点.上述功能有效,但我非常确定这不是最好的方法.有帮助吗?
最近,我们一群(电子工程专业学生 - 英国)在PIC16F84A微控制器编程的基础上开始掌握这一点.需要将两个8位数相乘,每个都没有已知的最小值/最大值.一位同学提出了以下想法.
multiply_numbers:
; Takes numbers in Num1 and Num2, and returns product in OutH:OutL
clrf OutH ; clear all non-input variables
clrf OutL
mult_loop
bcf STATUS,c ; clear carry bit
movfw Num2
addwf OutL ; add Num2 to OutL
btfsc STATUS,c ; check carry bit
incf OutH ; if set, increment OutH
decfsz Num1 ; decrement Num1
goto mult_loop ; if Num1 is not zero, repeat loop
return ; else return
Run Code Online (Sandbox Code Playgroud)
我觉得这虽然在代码行方面很短,但执行大数字可能需要相对较长的时间.我自己做了一些思考,开始沿着向右移动一个数字,向左移动另一个数字的路线,并沿着路径向输出添加左移数字一定次数到达输出最终答案.我做得不对,但后来偶然发现了这个问题,这让我想到了一个输入数字:
N = a_0 …
我正在 Arduino 上编写一些代码,该代码需要快速运行并对整数百分比进行粗略近似。
例如,给定一个数字,我想找到它的 90%、70% 或 30% 等。最明显的方法是乘以浮点,例如。x * 0.9;或 x * 0.3;但因为我需要速度,所以我想避免浮点计算。如果我只是除以 2 的幂,我会进行按位移位,但是是否有类似的技术可以使用整数来近似 90%、80% 等?
performance heuristics arduino integer-division integer-arithmetic
如何使用位移执行乘法36?是不是只能乘以2的幂?例如:
unsigned x = 4; // binary 00000000 00000000 00000000 00001000
unsigned y = x << 3; // multiply by 8, resulting in binary 00000000 ... 00100000
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有代码示例,用于在 8086 上将两个 16 位数字相乘并尝试将其更新为两个 32 位数字相乘。
start:
MOV AX,0002h ; 16 bit multiplicand
MOV BX,0008h ; 16 bit multiplier
MOV DX,0000h ; high 16 bits of multiplication
MOV CX,0000h ; low 16 bits of multiplication
MOV SI,10h ; loop for 16 times
LOOP:
MOV DI,AX
AND DI,01h
XOR DI,01h
JZ ADD
CONT:
RCR DX,1
RCR CX,1
SHR AX,1
DEC SI
CMP SI,0
JNZ LOOP
JMP END ; ignore here, it's not about multiplication.
ADD:
ADD DX,BX
JMP CONT
Run Code Online (Sandbox Code Playgroud)
上面的代码语句将两个 …
我发现这个线程使用shift和add进行乘法,我知道如何使它工作.但是,仅使用Shift和Rotate可以相乘.
可能重复:
如何仅使用位移和加法进行乘法和除法?
我必须编写函数来执行二进制减法,乘法和除法,而不使用除循环控制之外的任何算术运算符.我之前只用Java编写代码,所以我很难绕过这个问题.
从减法开始,我需要用原型编写一个函数
int bsub(int x, int y)
Run Code Online (Sandbox Code Playgroud)
我知道我需要将y转换为二进制补码以使其为负数并将其添加到x,但我只知道如何通过使用一个补码运算符并添加1,但我不能使用+运算符.
提供了badd函数,如果我能弄清楚如何制作负数,我将能够在bsub中实现它.badd的代码如下所示.提前感谢任何提示.
int badd(int x,int y){
int i;
char sum;
char car_in=0;
char car_out;
char a,b;
unsigned int mask=0x00000001;
int result=0;
for(i=0;i<32;i++){
a=(x&mask)!=0;
b=(y&mask)!=0;
car_out=car_in & (a|b) |a&b;
sum=a^b^car_in;
if(sum) {
result|=mask;
}
if(i!=31) {
car_in=car_out;
} else {
if(car_in!=car_out) {
printf("Overflow occurred\n");
}
}
mask<<=1;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)