Questions tagged [integer-division]

Anything related to the integer division operation, i.e. that special form of division which is performed between two integer numbers and which in math results in a quotient and a remainder. This is mostly relevant for languages which have specific integer data-types, for which the division is an integer division, or for languages having a specific operator/function to perform integer division.

Anything related to the integer division operation, i.e. that special form of division which is performed between two integer numbers and which in math results in a quotient and a remainder. This is mostly relevant for languages which have specific integer data-types, for which the division is an integer division, or for languages having a specific operator/function to perform integer division.

619 questions
3
votes
2 answers

How to divide a 128-bit dividend by a 64-bit divisor, where the dividend's bits are all 1's, and where I only need the 64 LSBs of the quotient?

I need to calculate (2128 - 1) / x. The divisor, x, is an unsigned 64-bit number. The dividend is composed of two unsigned 64-bit numbers (high and low), where both numbers are UINT64_MAX. I can only use 64-bit arithmetic and need it to be portable…
tgonzalez89
  • 621
  • 1
  • 6
  • 26
3
votes
1 answer

Large Integer division error in Julia when using UInt128 data type

I get incorrect results when I divide large integers of type UInt128. The error seems to occur about the same spot, significant figure wise, that a float64 will round its result. Using something simple, like dividing by 2, I can easily verify that…
3
votes
0 answers

Why is Modulo 2 optimization different from x&1?

Why is the assembly output of this code: code int f(int n) { return n % 2; } asm f: mov edx, edi shr edx, 31 lea eax, [rdi+rdx] and eax, 1 sub eax, edx ret different from…
user12722843
3
votes
1 answer

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

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…
3
votes
4 answers

In C why does the power function return 1 when the power is set to 1/2?

When I use the following code I tried to replicate the idea that sqrt(x) of something equals X^(1/2) pow(x, (1/2); It returned 1 no matter what value I entered. I already solved this issue with the sqrt function but wanted to know why this is…
ipefaur
  • 49
  • 4
3
votes
2 answers

Integer division in Dhall

I would like to compute the quotient of two Naturals. The requirement I need to fulfill is that I have a few configuration items that must be dynamically defined as shares of another (i.e. one container has X memory, two configuration keys for the…
3
votes
1 answer

Which kind of signed integer division corresponds to bit shift?

It is a familiar fact that when dividing integers by a power of two, a good compiler will strength-reduce this to bit shift. For example: int main(int argc, char **argv) { return argc/2; } Clang -O2 compiles this to: movl %ecx, %eax shrl …
rwallace
  • 31,405
  • 40
  • 123
  • 242
3
votes
4 answers

ASM 8086 division without div

I need to write in asm 8086 a program like b=a/6 but without the DIV instruction. I know how to do it with SAR but only 2,4,8,16... mov ax,a sar ax,1 ;//div a by 2 mov b,ax my question is how can I do it to div by 6?
3
votes
1 answer

What happens when we divide a 16-bit number with a 8-bit 1?

As far as I have heard, if we use div instruction with a 8-bit number then the quotient is a 8-bit number stored in AL and the remainder is again a 8-bit number stored in AH But what if we divide a 16-bit number by 1? I ended up having my…
3
votes
0 answers

right shift division for negative numbers

In case of unsigned integer u, we can easily get quotient with u>>k(k means divide by 2^k) But in case of negative numbers, it should be computed as (u+(1<>k I can see that u>>k does not work for negative numbers. But I am curious why we…
jwkoo
  • 2,393
  • 5
  • 22
  • 35
3
votes
2 answers

Can 128bit/64bit hardware unsigned division be faster in some cases than 64bit/32bit division on x86-64 Intel/AMD CPUs?

Can a scaled 64bit/32bit division performed by the hardware 128bit/64bit division instruction, such as: ; Entry arguments: Dividend in EAX, Divisor in EBX shl rax, 32 ;Scale up the Dividend by 2^32 xor rdx,rdx and rbx, 0xFFFFFFFF ;Clear any…
George Robinson
  • 1,500
  • 9
  • 21
3
votes
3 answers

Can consecutive truncating integer divisions be replaced with a multiplication?

In grade-school math with rational numbers, the expression (a / b) / c is equivalent to a / (b * c) by basic algebraic manipulation. Is the same true when / is truncating integer division as in C and most other languages? That is, can I replace a…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
3
votes
1 answer

Computing many remainders with remainder trees

I'm looking for fast a way to compute n mod x1, n mod x2, n mod x3, ... I found a an article about "remainder trees" which claims to do just that. However, I fail to see how is the above approach any better than naively computing each mod separately…
minmax
  • 493
  • 3
  • 10
3
votes
0 answers

Finding the best divider using integer arithmetic

We often have to find divider values in our clock framework which generate a clock rate closest to the target rate, i.e. ABS(target_rate - input_rate / best_div) should be minimal. We are limited to integer arithmetic and use the following…
bunq
  • 31
  • 2
3
votes
0 answers

Euler Project 401 at Maxima

Statement of the problem: The divisors of 6 are 1,2,3 and 6. The sum of the squares of these numbers is: 1 + 4 + 9 + 9 + 36 = 50 Allow sigma2(n) to represent the sum of the squares of the n-dividers. Thus, sigma2(6) = 50. And addition_sigma2 (n) is…
Programar
  • 31
  • 3