无论如何,我可以在不使用'/'' 运算符的情况下在C中实现除法吗?我可以使用bit aritmetic等吗? 谢谢, Sri
Hi, Is there anyway I can implement division in C without using the ''/'' operator? Can I use bit aritmetic or such? Thanks, Sri
推荐答案Sri schrieb: Sri schrieb: 无论如何,我可以在不使用''/''运算符的情况下在C中实现除法吗? 是: int divide(int dividend,int divisor) { int sign = 1; int result = 0; if(dividend == 0) 返回0; else if(dividend> 0){ sign * = -1; dividend * = -1; } if(divisor == 0){ / *句柄错误* / } else if(除数> 0){ sign * = -1; divisor * = -1; } while(dividend< 0){ dividend - = divisor; ++ result; } 返回结果; } 我可以使用bit aritmetic等吗? Is there anyway I can implement division in C without using the ''/'' operator? Yes: int divide (int dividend, int divisor) { int sign = 1; int result = 0; if (dividend == 0) return 0; else if (dividend > 0) { sign *= -1; dividend *= -1; } if (divisor == 0) { /* handle error */ } else if (divisor > 0) { sign *= -1; divisor *= -1; } while (dividend < 0) { dividend -= divisor; ++result; } return result; } Can I use bit aritmetic or such?
如果你愿意的话。 如果这是家庭作业或类似作品,请注明。 干杯 Michael - 电子邮件:我的是/ at / gmx / dot / de地址。
If you want to. If this is a homework assignment or similar, please state so. Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address.
" Sri" ;写道: "Sri" writes: 无论如何我可以在不使用''/''运算符的情况下在C中实现除法吗?我可以使用bit aritmetic等吗? Is there anyway I can implement division in C without using the ''/'' operator? Can I use bit aritmetic or such?
是的,你可以使用重复减法,这是什么分区真的是 左右。如果你希望希望,你可能会更加模糊并使用按位操作。
Yes, you could use repeated subtraction, which is what division is really about. You could be even more obscure and use bitwise operations if you wished.
谢谢迈克尔。这不是* * *作业*作业。这是由朋友拍摄的,这是我的大脑。 Thanks Michael. This is *not* a *homework* assignment. This was shot at me by a friend and it was picking my brain.