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

How can I add numbers in a Bash script?

I have this Bash script and I had a problem in line 16. How can I take the previous result of line 15 and add it to the variable in line 16? #!/bin/bash num=0 metab=0 for ((i=1; i<=2; i++)); do for j in `ls output-\$i-*`; do echo "\$j" …
Nick
• 7,567
• 3
• 15
• 4

Unexpected results when working with very big integers on interpreted languages

I am trying to get the sum of 1 + 2 + ... + 1000000000, but I'm getting funny results in PHP and Node.js. PHP \$sum = 0; for(\$i = 0; \$i <= 1000000000 ; \$i++) { \$sum += \$i; } printf("%s", number_format(\$sum, 0, "", "")); //…
Baba
• 94,024
• 28
• 166
• 217

Times-two faster than bit-shift, for Python 3.x integers?

I was looking at the source of sorted_containers and was surprised to see this line: self._load, self._twice, self._half = load, load * 2, load >> 1 Here load is an integer. Why use bit shift in one place, and multiplication in another? It seems…
hilberts_drinking_problem
• 11,322
• 3
• 22
• 51

Is unsigned integer subtraction defined behavior?

I have come across code from someone who appears to believe there is a problem subtracting an unsigned integer from another integer of the same type when the result would be negative. So that code like this would be incorrect even if it happens to…
user14554

Determining if a number is either a multiple of ten or within a particular set of ranges

I have a few loops that I need in my program. I can write out the pseudo code, but I'm not entirely sure how to write them logically. I need - if (num is a multiple of 10) { do this } if (num is within 11-20, 31-40, 51-60, 71-80, 91-100) { do this…
user3419168
• 1,135
• 2
• 8
• 16

In C# integer arithmetic, does a/b/c always equal a/(b*c)?

Let a, b and c be non-large positive integers. Does a/b/c always equal a/(b * c) with C# integer arithmetic? For me, in C# it looks like: int a = 5126, b = 76, c = 14; int x1 = a / b / c; int x2 = a / (b * c); So my question is: does x1 == x2 for…
Jason Crease
• 1,896
• 17
• 17

Is masking before unsigned left shift in C/C++ too paranoid?

This question is motivated by me implementing cryptographic algorithms (e.g. SHA-1) in C/C++, writing portable platform-agnostic code, and thoroughly avoiding undefined behavior. Suppose that a standardized crypto algorithm asks you to implement…
Nayuki
• 17,911
• 6
• 53
• 80

Compiler optimizations may cause integer overflow. Is that okay?

I have an int x. For simplicity, say ints occupy the range -2^31 to 2^31-1. I want to compute 2*x-1. I allow x to be any value 0 <= x <= 2^30. If I compute 2*(2^30), I get 2^31, which is an integer overflow. One solution is to compute 2*(x-1)+1.…
mbang
• 855
• 2
• 9

Java Primitives range calculation

In Java when we declare short number=1024*1024*1024; it will give compile time error but short number=1024 * 1024 * 1024 * 1024; compiles fine. Why does this happen?
Jekin Kalariya
• 3,475
• 2
• 20
• 32

How can I detect integer overflow on 32 bits int?

I know such topic was asked several times, but my question is about overflow on full 32 bits of int. For example: 11111111111111111111111111111111 + 00000000000000000000000000000001 = 00000000000000000000000000000000 //overflow! I found…
ashur
• 4,177
• 14
• 53
• 85

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

The following Python 3.x integer multiplication takes on average between 1.66s and 1.77s: import time start_time = time.time() num = 0 for x in range(0, 10000000): # num += 2 * (x * x) num += 2 * x * x print("--- %s seconds ---" %…
Waqas Gondal
• 467
• 6
• 15

Panicked at 'attempt to subtract with overflow' when cycling backwards though a list

I am writing a cycle method for a list that moves an index either forwards or backwards. The following code is used to cycle backwards: (i-1)%list_length In this case, i is of the type usize, meaning it is unsigned. If i is equal to 0, this leads…
lmartens
• 1,432
• 2
• 12
• 19

Why is ushort + ushort equal to int?

Previously today I was trying to add two ushorts and I noticed that I had to cast the result back to ushort. I thought it might've become a uint (to prevent a possible unintended overflow?), but to my surprise it was an int (System.Int32). Is there…
lesderid
• 3,388
• 8
• 39
• 65

Is there a cost to entering and exiting a C# checked block?

Consider a loop like this: for (int i = 0; i < end; ++i) // do something If I know that i won't overflow, but I want a check against overflow, truncation, etc., in the "do something" part, am I better off with the checked block inside or…
Edward Brey
• 40,302
• 20
• 199
• 253