integer-arithmetic Questions

3

Solved

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, me...
Saintsimonianism asked 23/12, 2016 at 12:24

15

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++)); ...
Trimetallic asked 14/6, 2011 at 19:25

2

Is there an easy, efficient and correct (i.e. not involving conversions to/from double) way to do floored integer division (like e.g. Python offers) in C#. In other words, an efficient version of ...
Marleah asked 21/1, 2015 at 4:31

4

Solved

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

1

Solved

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 ti...
Centrum asked 7/5, 2022 at 12:57

5

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 ove...
Rosenthal asked 11/11, 2017 at 0:5

5

Solved

Are there any branch-less or similar hacks for clamping an integer to the interval of 0 to 255, or a double to the interval of 0.0 to 1.0? (Both ranges are meant to be closed, i.e. endpoints are in...

6

Solved

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 ...
Sycee asked 28/8, 2011 at 13:59

2

Solved

Assume we have 3 bits to play with. I am going to represent plus and minus 3 in 2's complement: +3 = 0b011 -3 = 0b101 When doing addition you always have a dangling bit when an overflow happens li...
Vashtivashtia asked 29/7, 2021 at 21:9

4

Solved

I'm trying to implement a fast primality test for Rust's u32 and u64 datatypes. As part of it, I need to compute (n*n)%d where n and d are u32 (or u64, respectively). While the result can easily f...

9

Solved

Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..
Jemmy asked 11/10, 2009 at 5:35

1

Solved

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 ...
Bassorilievo asked 20/1, 2021 at 20:50

4

Solved

Assuming that uint is the largest integral type on my fixed-point platform, I have: uint func(uint a, uint b, uint c); Which needs to return a good approximation of a * b / c. The value of c is gr...
Danelle asked 28/10, 2020 at 7:51

1

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...
Homoeo asked 25/7, 2020 at 18:17

17

Solved

How can I subtract two integers in C without the - operator?
Demmy asked 31/3, 2009 at 7:30

1

Solved

Does anyone know how to do arithmetic assignment for integer variables in set_fact module? Currently, I managed to do it by using String variable using Jinja2 template like this: - set_fact: fla...
Canso asked 16/4, 2020 at 8:20

1

Solved

EDIT2: As @ShadowRanger pointed out, this is a Numpy phenomenon, not Python. But when doing the calculations in Python with list comprehensions (so x+y becomes [a+b for a,b in zip(x,y)]) then all a...

2

Solved

Why is it that Rust's u64 primitive expects a u32 exponent? error[E0308]: mismatched types --> src/protagonists.rs:13:25 | 13 | return root.pow(self.secret) % prime; | ^^^^^^^^^^^ expected u...
Cumae asked 19/7, 2019 at 21:2

8

I have to store an integer value that is larger than the maximum value for the long datatype. How would I store and manipulate this value in memory? Please illustrate it through an example, if pos...
Caitiff asked 12/2, 2010 at 15:35

2

Solved

Many lossless algorithms in signal processing require an evaluation of the expression of the form ⌊ a / 2b ⌋, where a, b are signed (a possibly negative, b non-negative) integer...

4

Solved

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 ...
Wasserman asked 1/12, 2018 at 12:40

1

Solved

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 m...
Sankey asked 17/10, 2018 at 8:42

5

Solved

I know such topic was asked several times, but my question is about overflow on full 32 bits of int. For example: 11111111111111111111111111111111 + 00000000000000000000000000000001 = 00000000...

10

Solved

From the comments on my answer here, the question was asked (paraphrase): Write a Python program to find a 4 digit whole number, that when multiplied to itself, you get an 8 digit whole numbe...
Buckjump asked 5/4, 2018 at 12:59

3

Solved

int plus unsigned int returns an unsigned int. Should it be so? Consider this code: #include <boost/static_assert.hpp> #include <boost/typeof/typeof.hpp> #include <boost/type_trait...
Carburize asked 6/4, 2012 at 18:28

© 2022 - 2024 — McMap. All rights reserved.