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
755
votes
13 answers

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
193
votes
36 answers

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
162
votes
1 answer

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…
122
votes
6 answers

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
104
votes
13 answers

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
81
votes
6 answers

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
76
votes
5 answers

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
64
votes
4 answers

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.…
62
votes
2 answers

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
47
votes
5 answers

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
42
votes
4 answers

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 ---" %…
38
votes
2 answers

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
38
votes
5 answers

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
29
votes
4 answers

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
26
votes
5 answers

What does -fwrapv do?

Can anyone provide some code examples that act differently when compiled with -fwrapv vs without? The gcc documentation says that -fwrapv instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication…
ktal
  • 263
  • 1
  • 3
  • 4
1
2 3
30 31