Questions tagged [integer-arithmetic]

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. `int` or `long` in C, C++ or Java).

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. int or long in C, C++ or Java).

452 questions
5
votes
1 answer

Unexpected behaviour on underflow of unsigned char

The following program: #include #include int main () { unsigned char result1 {0}; unsigned char result2 {0}; result1 = (result1 - 1) % 8; result2 = result2 - 1; result2 = result2 % 8; std::cout <<…
JosF
  • 53
  • 4
5
votes
1 answer

How to unset N right-most set bits

There is a relatively well-known trick for unsetting a single right-most bit: y = x & (x - 1) // 0b001011100 & 0b001011011 = 0b001011000 :) I'm finding myself with a tight loop to clear n right-most bits, but is there a simpler algebraic…
qdot
  • 6,195
  • 5
  • 44
  • 95
5
votes
1 answer

How can I descale x by n/d, when x*n overflows?

My problem is limited to unsigned integers of 256 bits. I have a value x, and I need to descale it by the ratio n / d, where n < d. The simple solution is of course x * n / d, but the problem is that x * n may overflow. I am looking for any…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
5
votes
1 answer

Why do arithmetic operations on bytes return an int in kotlin?

Consider this code: val x1: Byte = 0x00 val x2: Byte = 0x01 val x3: Byte = x1 + x2; This gives a compile error, because the result of adding 2 Bytes is an Int. To work around this, I need to manually cast the result back into a byte: val x3: Byte…
Tiddo
  • 6,331
  • 6
  • 52
  • 85
5
votes
1 answer

Dividing two double integers in Forth

I'm using Gforth, and I've looked for a standard Forth word for dividing two double integers, or at least a mixed division of a double integer by a single integer, yet supporting double integers as a result. There doesn't seem to be one. SM/REM,…
viuser
  • 953
  • 6
  • 19
5
votes
3 answers

How to add numbers without +

I was asked this question on the interview. I didn't answer and actually I don't understand how it works. int add(int x, int y) { while (y != 0) { int carry = x & y; x = x ^ y; y = carry << 1; } return…
Alupkers
  • 223
  • 1
  • 9
5
votes
7 answers

Explanation for this function's output

I am doing review questions which ask me "What is the output of the following," and I am having some trouble understanding something about this function: int a = 1, b = 1, c = -1; c = --a && b++; printf("%d %d %d", a, b, c); The output is 010. My…
Bobbis
  • 137
  • 1
  • 7
5
votes
3 answers

signed and unsigned arithmetic implementation on x86

C language has signed and unsigned types like char and int. I am not sure, how it is implemented on assembly level, for example it seems to me that multiplication of signed and unsigned would bring different results, so do assembly do both…
user2214913
  • 1,441
  • 2
  • 19
  • 29
5
votes
2 answers

Java8 unsigned arithmetic

Java 8 is widely reported to have library support for unsigned integers. However, there seem to be no articles explaining how to use it and how much is possible. Some functions like Integer.CompareUnsigned are easy enough to find and seem to do what…
Jannis Froese
  • 1,347
  • 2
  • 10
  • 23
5
votes
2 answers

How can I calculate (A*B)%C for A,B,C <= 10^18, in C++?

For example, A=10^17, B=10^17, C=10^18. The product A*B exceeds the limit of long long int. Also, writing ((A%C)*(B%C))%C doesn't help.
user3158621
  • 53
  • 1
  • 4
5
votes
2 answers

What gives Lisp its excellent math performance?

I am reading this in a Lisp textbook: Lisp can perform some amazing feats with numbers, especially when compared with most other languages. For instance, here we’re using the function expt to calculate the fifty-third power of 53: CL> (expt 53 53)…
johnbakers
  • 24,158
  • 24
  • 130
  • 258
5
votes
3 answers

C array arithmetic and pointers

Possible Duplicate: In C arrays why is this true? a[5] == 5[a] I am reading through a tutorial on C and I came across this syntax: int doses[] = {1, 3, 2, 1000}; doses[3] == *(doses + 3) == *(3 + doses) == 3[doses] Now the point is to get the…
Andy
  • 10,553
  • 21
  • 75
  • 125
4
votes
1 answer

Time it takes to square in python

I was wondering whether x**2 or x*x is faster def sqr(x): for i in range (20): x = x**2 return x def sqr_(x): for i in range (20): x = x*x return x When I time it, this is what I get: The time it takes for x**2:…
goku
  • 167
  • 10
4
votes
2 answers

What is the operations order of these two functions

I was writing a function to calculate the volume of a sphere when I encounter a problem and I don't know why if I change 4/3 * PI to PI * 4/3 I got different result. What is the order of evaluation, if I use parenthesis like (4/3) * PI and PI *…
4
votes
1 answer

C++ last digit of a random sequence of powers

I realise that there are several topics already covering this. But my question is not regarding how to build such an algorithm, rather in finding what mistake I have made in my implementation that's causing a single test out of dozens to fail. The…
Chris
  • 65
  • 5