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).

381 questions
636
votes
11 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
  • 6,377
  • 3
  • 12
  • 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
  • 89,415
  • 27
  • 158
  • 212
153
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…
108
votes
4 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
105
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,137
  • 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,703
  • 14
  • 17
73
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
  • 16,655
  • 5
  • 47
  • 75
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,255
  • 2
  • 18
  • 31
40
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 ---" %…
39
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
  • 3,661
  • 12
  • 46
  • 76
32
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,081
  • 7
  • 36
  • 62
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
  • 35,877
  • 14
  • 173
  • 224
26
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,272
  • 2
  • 10
  • 18
25
votes
1 answer

Passing arbitrary-sized integers from Prolog to C

Right now, I'm learning how to interface SICStus Prolog with C code. I would like to have/use/see a C implementation of "Hamming weight" of arbitrary-sized integers in SICStus Prolog version 4. It seems to me that I need C functions for testing term…
repeat
  • 19,449
  • 4
  • 51
  • 152
25
votes
7 answers

Is arithmetic overflow equivalent to modulo operation?

I need to do modulo 256 arithmetic in C. So can I simply do unsigned char i; i++; instead of int i; i=(i+1)%256;
avmohan
  • 1,549
  • 1
  • 18
  • 35
1
2 3
25 26