【发布时间】:2015-04-30 19:27:59
【问题描述】:
我正在查看 Wikipedia 中的 binary integer division with remainder 部分,想知道如何将其翻译成另一种语言,例如 C?
if D == 0 then error(DivisionByZeroException) end
Q := 0 -- initialize quotient and remainder to zero
R := 0
for i = n-1...0 do -- where n is number of bits in N
R := R << 1 -- left-shift R by 1 bit
R(0) := N(i) -- set the least-significant bit of R equal to bit i of the numerator
if R >= D then
R := R - D
Q(i) := 1
end
end
我知道如何翻译其中的大部分内容并进行位移,但是当它们是整数而不是函数或函数指针时,我该怎么做?抱歉这个基本问题,这个小部分让我感兴趣。
【问题讨论】:
-
屏蔽和使用按位运算。 0010 | 1000 = 1010。您只需设置其中一位。进一步解释:cprogramming.com/tutorial/bitwise_operators.html 和来自这里的一个:stackoverflow.com/questions/10493411/what-is-masking
标签: c binary division translate integer-division