integer-arithmetic

Java8 unsigned arithmetic

梦想与她 提交于 2021-02-07 14:42:42
问题 Java 8 is widely reported to have library support for unsigned integers. However, there seem to be no articles explaining how to use it and how much is possible. Some functions like Integer.CompareUnsigned are easy enough to find and seem to do what one would expect. However, I fail to write even a simple loop that loops over all powers of two within the range of unsigned long. int i = 0; for(long l=1; (Long.compareUnsigned(l, Long.MAX_VALUE*2) < 0) && i<100; l+=l) { System.out.println(l); i+

Java8 unsigned arithmetic

佐手、 提交于 2021-02-07 14:41:50
问题 Java 8 is widely reported to have library support for unsigned integers. However, there seem to be no articles explaining how to use it and how much is possible. Some functions like Integer.CompareUnsigned are easy enough to find and seem to do what one would expect. However, I fail to write even a simple loop that loops over all powers of two within the range of unsigned long. int i = 0; for(long l=1; (Long.compareUnsigned(l, Long.MAX_VALUE*2) < 0) && i<100; l+=l) { System.out.println(l); i+

How can I descale x by n/d, when x*n overflows?

被刻印的时光 ゝ 提交于 2021-02-05 05:54:06
问题 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 that x * n may overflow. I am looking for any arithmetic trick which may help in reaching a result as accurate as possible. Dividing each of n and d by gcd(n, d) before calculating x * n / d does not guarantee success. Is there any process (iterative or other) which i can use in order to solve

Exact sum of a long array

一曲冷凌霜 提交于 2021-01-27 04:24:07
问题 In order to get the exact sum of a long[] I'm using the following snippet. public static BigInteger sum(long[] a) { long low = 0; long high = 0; for (final long x : a) { low += (x & 0xFFFF_FFFFL); high += (x >> 32); } return BigInteger.valueOf(high).shiftLeft(32).add(BigInteger.valueOf(low)); } It works fine by processing the numbers split in two halves and finally combining the partial sums. Surprisingly, this method works too: public static BigInteger fastestSum(long[] a) { long low = 0;

How can I compute a * b / c when both a and b are smaller than c, but a * b overflows?

不想你离开。 提交于 2021-01-26 11:48:27
问题 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 greater than both the value of a and the value of b . So we know for sure that the value of a * b / c would fit in a uint . However, the value of a * b itself overflows the size of a uint . So one way to compute the value of a * b / c would be: return a / c * b; Or even: if (a > b) return a / c * b;

How to perform all possible combinations of arithmetic operations on 3 integers?

风流意气都作罢 提交于 2021-01-21 11:59:49
问题 Suppose I have three integers. I want to get a list of all possible values obtained by performing all 16 (4x4 of *, /, +, - ) operations among between them. Like if I have 3 4 1 , we should get values 1, 2, 6, 7, 8, 9, 11, 12, 13, 15 and 16. That is, res = num1 (op1) num2 (op2) num3 where operators are: ["**", "*/", "*+", "*-", "/*", "//", "/+", "/-", "+*", "+/", "++", "+-", "-*", "-/", "-+", "--"] However, the catch is that division can only be possible if x%y==0 i.e. divisor is factor of

How to perform all possible combinations of arithmetic operations on 3 integers?

a 夏天 提交于 2021-01-21 11:56:44
问题 Suppose I have three integers. I want to get a list of all possible values obtained by performing all 16 (4x4 of *, /, +, - ) operations among between them. Like if I have 3 4 1 , we should get values 1, 2, 6, 7, 8, 9, 11, 12, 13, 15 and 16. That is, res = num1 (op1) num2 (op2) num3 where operators are: ["**", "*/", "*+", "*-", "/*", "//", "/+", "/-", "+*", "+/", "++", "+-", "-*", "-/", "-+", "--"] However, the catch is that division can only be possible if x%y==0 i.e. divisor is factor of

How to implement arithmetic right shift in C

我怕爱的太早我们不能终老 提交于 2021-01-21 07:47:17
问题 Many lossless algorithms in signal processing require an evaluation of the expression of the form ⌊ a / 2 b ⌋, where a , b are signed ( a possibly negative, b non-negative) integers and ⌊·⌋ is the floor function. This usually leads to the following implementation. int floor_div_pow2(int numerator, int log2_denominator) { return numerator >> log2_denominator; } Unfortunately, the C standard states that the result of the >> operator is implementation-defined if the left operand has a signed

How many least-significant bits are the same for both an unsigned and a signed multiplication?

那年仲夏 提交于 2020-01-15 09:44:12
问题 ARM processors, for example, have a mul instruction, which performs a 32-bit x 32-bit multiplication and returns the least-significant 32-bits of the result. They also have umull and smull instructions, which again do a 32-bit x 32-bit multiplication, but return a full 64-bit result. umull does an unsigned multiplication, and smull does a signed multiplication. Why is it not necessary to have separate unsigned and signed versions of mul ? In the case of a 32-bit x 32-bit multiplication,

Is arithmetic overflow equivalent to modulo operation?

对着背影说爱祢 提交于 2020-01-12 04:09:46
问题 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; 回答1: No. There is nothing that guarantees that unsigned char has eight bits. Use uint8_t from <stdint.h> , and you'll be perfectly fine. This requires an implementation which supports stdint.h : any C99 compliant compiler does, but older compilers may not provide it. Note: unsigned arithmetic never overflows, and behaves as "modulo 2^n". Signed arithmetic overflows with undefined