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

Assigning a variable during arithmetic in Java?

My professor gave us this java snippet during a lecture and I don't understand why it outputs 12. int b = 9; b = b + (b = 3); System.out.println( "b = " + b ); My thinking is that since parentheses give operations precedence, b would be…
S. King
  • 53
  • 3
3
votes
2 answers

Modular Inverse Built-In, C++

There is a very nice way to find a modular inverse (that is, such b that ab ≡ 1 (mod m) for given a and m) in python-3.8: b = pow(a, -1, m) pow is built-in in python-3.8. Is there something like this in c++?
3
votes
1 answer

Long multiplication of a pair of uint64 values

How can I multiply a pair of uint64 values safely in order to get the result as a pair of LSB and MSB of the same type? typedef struct uint128 { uint64 lsb; uint64 msb; }; uint128 mul(uint64 x, uint64 y) { uint128 z = {0, 0}; z.lsb…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
3
votes
4 answers

Subtract or add constant to large array

I have a large uint8_t array (size = 1824 * 942). I want to do the same operation to each element. Particularly I need to subtract -15 from each element. This array is refreshed 20 times per second, so time is an issue and I'm avoiding loops over…
Ivan
  • 1,352
  • 2
  • 13
  • 31
3
votes
1 answer

How to properly add a negative number to a size_t

I want to support negative indexing on my list implementation, the way I want to handle this (I know there may be better way to handle negative indexing) is by converting negative indexes into their positive equivalent by adding the negative value…
user338717
  • 77
  • 5
3
votes
2 answers

BigQuery: How to represent integer of type Long in Bigquery?

I have a dataset that contains integer of type “Long”. I need to analyse that dataset by doing some arithmetic operations on it. What are the possible ways to do that? I have tried casting to FLOAT but then I can't use some operations like bitwise…
john
  • 2,324
  • 3
  • 20
  • 37
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
2 answers

Why does the result of an expression depend on where the expression is placed?

I am currently working on a more complex program, and I came across a very weird syntax error best demonstrated with the following minimal example: #include int main(int argc, char *argv[]){ char c = 1 + '0'; std::cout <<…
john01dav
  • 1,842
  • 1
  • 21
  • 40
3
votes
1 answer

multiprecision unsigned subtraction in C

I am trying to implement multi-precision unsigned subtraction over a finite field (p=2^191-19) in C, but I cannot figure out how to deal with borrow bit! My operands are represented in radix-2^16 as: typedef unsigned long long T[12]; which means…
A23149577
  • 2,045
  • 2
  • 40
  • 74
3
votes
3 answers

Implementing equality function with basic arithmetic operations

Given positive-integer inputs x and y, is there a mathematical formula that will return 1 if x==y and 0 otherwise? I am in the unfortunate position of having to use a tool that only allows me to use the following symbols: numerals 0-9; decimal point…
user2384183
3
votes
3 answers

CPUs not like humans? 0 + 0 not easier than 10E12 + 9E15?

My kid asked me a funny question yesterday: Dad, does a computer have trouble adding / multiplying large numbers like I do? Does it take longer? I laughed and answered of course not, computers are equally fast with any numbers, they are that…
InBetween
  • 32,319
  • 3
  • 50
  • 90
3
votes
1 answer

How to generate random arithmetic expressions for game

i would like to know if you can help me with this problem for my game. I'm currently using lots of switch, if-else, etc on my code and i'm not liking it at all. I would like to generate 2 random arithmethic expressions that have one of the forms…
kugelblitz
  • 33
  • 5
3
votes
4 answers

Large Integer Arithmetic - how to implement modulo?

I want to implement my own (simple) large/arbitrary integer precision arithmetic, first in Java (cause I am more familiar with the syntax), then rewrite it to C. I have addition, subtraction and multiplication for numbers of infinite length and now…
luuksen
  • 1,357
  • 2
  • 12
  • 38
3
votes
1 answer

Equivalence of two bitwise operations

The following two C functions are equivalent: unsigned f(unsigned A, unsigned B) { return (A | B) & -(A | B); } unsigned g(unsigned A, unsigned B) { unsigned C = (A - 1) & (B - 1); return (C + 1) & ~C; } My question is: why are they…
3
votes
1 answer

Overflow-aware implementation of a kalman filter

I'm trying to implement a kalman filter to obtain the orientation of an object, using an 3 axis accelerometer and a 3 axis gyroscope as sensors. Choosing the dynamic model for the predict phase of this filter is straight forward, it's: new_angle =…