integer-division

Logarithmic time integer division using bit shift addition and subtraction only

心已入冬 提交于 2021-02-18 17:13:49
问题 I was asked to implement integer division with logarithmic time complexity using only bit shifts, additions and subtractions. I can see how I can deal with a divisor which is a power of 2, but how can I deal with an odd divisor, such that the time remains logarithmic? Is it even possible? EDIT: a way to do it in a time complexity that isn't logarithmic but still better than linear will also be welcomed. Thanks 回答1: It's just like doing long division on paper but in binary. You shift bits from

The sum of n terms of fractional expression is an integer when it should be a float

╄→гoц情女王★ 提交于 2021-02-11 14:44:10
问题 In the following code why don't I get a valid result unless I put term = 1.0/n and not when term = 1/n . I have declared term as float, Shouldn't that be enough? #include <stdio.h> int main() { float sum = 0, term; int n, i; printf("enter the value of n:\n"); scanf("%d", &n); term = 1.0 / n; for(i = 1; i <= n; i++) { sum = term + sum; } printf("Sum = %.3f\n", sum); return 0; } 回答1: In the following code why don't I get a valid result unless I put term = 1.0/n and not when term = 1/n . I have

Fast method to multiply integer by proper fraction without floats or overflow

一曲冷凌霜 提交于 2021-02-07 05:14:54
问题 My program frequently requires the following calculation to be performed: Given: N is a 32-bit integer D is a 32-bit integer abs(N) <= abs(D) D != 0 X is a 32-bit integer of any value Find: X * N / D as a rounded integer that is X scaled to N/D (i.e. 10 * 2 / 3 = 7) Obviously I could just use r=x*n/d directly but I will often get overflow from the x*n . If I instead do r=x*(n/d) then I only get 0 or x due to integer division dropping the fractional component. And then there's r=x*(float(n)/d)

Fast method to multiply integer by proper fraction without floats or overflow

痞子三分冷 提交于 2021-02-07 05:14:38
问题 My program frequently requires the following calculation to be performed: Given: N is a 32-bit integer D is a 32-bit integer abs(N) <= abs(D) D != 0 X is a 32-bit integer of any value Find: X * N / D as a rounded integer that is X scaled to N/D (i.e. 10 * 2 / 3 = 7) Obviously I could just use r=x*n/d directly but I will often get overflow from the x*n . If I instead do r=x*(n/d) then I only get 0 or x due to integer division dropping the fractional component. And then there's r=x*(float(n)/d)

Remainder after division by zero

…衆ロ難τιáo~ 提交于 2021-02-05 07:36:41
问题 I know that division by zero is not allowed in math, but can I use modulo by zero and what answer should I get? For example 10%0 = ? 5%0 = ? 回答1: The standard defines it as "undefined". In nearly all processors, the modulo is performed by the same functionality as divide. In modern larger processors, it's an instruction (x86 for example). Most often, this instruction will cause a trap when dividing by zero, and this applies whether the code is "intending to use the modulo" or "quotient" part.

Remainder after division by zero

你。 提交于 2021-02-05 07:36:18
问题 I know that division by zero is not allowed in math, but can I use modulo by zero and what answer should I get? For example 10%0 = ? 5%0 = ? 回答1: The standard defines it as "undefined". In nearly all processors, the modulo is performed by the same functionality as divide. In modern larger processors, it's an instruction (x86 for example). Most often, this instruction will cause a trap when dividing by zero, and this applies whether the code is "intending to use the modulo" or "quotient" part.

What is signed division(idiv) instruction?

房东的猫 提交于 2021-01-19 07:02:21
问题 In intel instruction, idiv(integer divsion) means signed division. I got the result of idiv , but I don't quite understand the result. - Example 0xffff0000 idiv 0xffff1100 - My wrong prediction As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because... 0xffff0000 / 0xffff1100 = 0 0xffff0000 % 0xffff1100 = 0xffff0000 - However, the result was... Before idiv eax 0xffff0000 # dividend esi 0xffff1100 # divisor edx 0x0 After idiv eax 0xfffeedcc # quotient edx 0x7400

What is signed division(idiv) instruction?

谁说我不能喝 提交于 2021-01-19 07:01:26
问题 In intel instruction, idiv(integer divsion) means signed division. I got the result of idiv , but I don't quite understand the result. - Example 0xffff0000 idiv 0xffff1100 - My wrong prediction As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because... 0xffff0000 / 0xffff1100 = 0 0xffff0000 % 0xffff1100 = 0xffff0000 - However, the result was... Before idiv eax 0xffff0000 # dividend esi 0xffff1100 # divisor edx 0x0 After idiv eax 0xfffeedcc # quotient edx 0x7400

EMU8086 dividing 32 bit number by a 16 bit number gives unexpected 0 remainder

天大地大妈咪最大 提交于 2021-01-03 07:07:52
问题 I was trying to divide (Unsigned) 8A32F4D5 by C9A5 using emu8086 tool. I expected the quotient to be AF73H and the remainder be 94B6H . After writing the following code, I was getting correct quotient but the remainder became 0000h . Am I missing something? .MODEL SMALL .STACK 100H .DATA .CODE MAIN PROC ; initialize DS MOV AX,@DATA MOV DS,AX ; enter your code here MOV DX, 8A32H MOV AX, 0F4D5H MOV BX, 0C9A5H DIV BX ;exit to DOS MOV AX,4C00H INT 21H MAIN ENDP END MAIN The output in EMU8086: 回答1

EMU8086 dividing 32 bit number by a 16 bit number gives unexpected 0 remainder

蓝咒 提交于 2021-01-03 07:04:46
问题 I was trying to divide (Unsigned) 8A32F4D5 by C9A5 using emu8086 tool. I expected the quotient to be AF73H and the remainder be 94B6H . After writing the following code, I was getting correct quotient but the remainder became 0000h . Am I missing something? .MODEL SMALL .STACK 100H .DATA .CODE MAIN PROC ; initialize DS MOV AX,@DATA MOV DS,AX ; enter your code here MOV DX, 8A32H MOV AX, 0F4D5H MOV BX, 0C9A5H DIV BX ;exit to DOS MOV AX,4C00H INT 21H MAIN ENDP END MAIN The output in EMU8086: 回答1