integer-arithmetic

Beginner Java programmer arithmetic error

我的梦境 提交于 2019-12-25 02:08:49
问题 I've recently embarked into Java programming and consider myself a programming novice. It seem that I'm having an issue with my source code arithmetic. I have verified all the nested if-else statements and they all work up to the final else statement's arithmetic. It doesn't calculating correctly I have set the arithmetic up just as the above if-else statements. the else statement is suppose to subtract 40 from the amount and then apply 1 percent charge. I have tried for the else statement

Adding two numbers without using the addition operator

走远了吗. 提交于 2019-12-25 01:56:09
问题 In c ~ is 1's complement operator. This is equivalent to: ~a = -b + 1 So, a - ~b -1 = a-(-b + 1) + 1 = a + b – 1 + 1 = a + b Can anyone explains this to me? 回答1: From elementary school math we know a = -(-a); From twos complement we know that -a = (~a) + 1 (invert and add one) so we know that a + b = a - (-b) elementary math = a - (~b + 1) twos complement = a - (~b) - 1 distribute the negative (elementary math) 回答2: You are right that ~ is always 1's complement (aka bitwise not) in c. Where

Montgomery multiplication VHDL Implementation

若如初见. 提交于 2019-12-24 10:49:48
问题 I am trying to create a modular arithmetic operation in this case: x*y mod n As far as I have read the fastest way to do it is using the Montgomery multiplication, but I cant understand how that is actually done in other to implement it in hardware using VHDL. Has someone been able to do it or have any library that enables me to use it? 回答1: A basic shift and add modular multiplication can be found here as a part of this open cores project. Here's another project that is using the Montgomery

Ambiguity in long integer arithmetic?

隐身守侯 提交于 2019-12-24 08:01:41
问题 Have a look at the following piece of code: #include <stdio.h> int main(void) { int a; a = 2147483647; printf("a + 1 = %d \t sizeof (a + 1) = %lu\n", a + 1, sizeof (a + 1)); printf("a + 1L = %ld \t sizeof (a + 1L) = %lu\n", a + 1L, sizeof (a + 1L)); a = -1; printf("a + 1 = %d \t sizeof (a + 1) = %lu\n", a + 1, sizeof (a + 1)); printf("a + 1L = %ld \t sizeof (a + 1) = %lu\n", a + 1L, sizeof (a + 1L)); //why a + 1L does not yield long integer ? return 0; } This results in the following output:

numeric values bigger than ULLONG_MAX

百般思念 提交于 2019-12-24 02:06:52
问题 I need to store and use numeric values bigger than ULLONG_MAX . I will need to make arithmetic operations with those values, so I think store as char** is not a option. Is there a way to dynamically create an additional long prefix in those cases? Thank you all. Based on responses, Very helpful but I doubt about the best performance option. piokuc quote a performance link, but it's unclear. In this time I'm between GMP or MPIR, there is some one faster? 回答1: You need to use one of the

Integral solution to equation `a + bx = c + dy`

a 夏天 提交于 2019-12-23 09:17:16
问题 In the equation a + bx = c + dy , all variables are integers. a , b , c , and d are known. How do I find integral solutions for x and y ? If I'm thinking right, there will be an infinite number of solutions, separated by the lowest common multiple of b and d , but all I need is one solution, and I can calculate the rest. Here's an example: a = 2 b = 3 c = 4 d = 5 a + bx: (2, 5, 8, 11, 14) c + dy: (4, 9, 14, 19, 24) a + bx intersects c + dy at 14, so: x = 4 y = 2 Right now, I'm looping through

Java Scientific Calculator Regular Expression

跟風遠走 提交于 2019-12-23 03:17:21
问题 What will be the regular expression in java for like this expression (3+2)+23/12-(43/54) in which left parentheses is create than user will be able to put right one and if left parentheses is not created than user will not able to put right parentheses .And if left parentheses is created 3 time than user will be able to just put right parentheses 3 times only to close the expression which is open by left parentheses. Thanks 回答1: In a nutshell, this is not possible using standard regular

From a loop index k, obtain pairs i,j with i < j?

坚强是说给别人听的谎言 提交于 2019-12-21 05:12:19
问题 I need to traverse all pairs i,j with 0 <= i < n , 0 <= j < n and i < j for some positive integer n . Problem is that I can only loop through another variable, say k . I can control the bounds of k . So the problem is to determine two arithmetic methods, f(k) and g(k) such that i=f(k) and j=g(k) traverse all admissible pairs as k traverses its consecutive values. How can I do this in a simple way? 回答1: I think I got it (in Python): def get_ij(n, k): j = k // (n - 1) # // is integer

In C# integer arithmetic, does a/b/c always equal a/(b*c)?

只谈情不闲聊 提交于 2019-12-20 09:32:28
问题 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 all a, b and c? 回答1: Let \ denote integer division (the C# / operator between two int s) and let / denote usual math division. Then, if x,y,z are positive integers and we are ignoring overflow , (x \ y) \ z = floor(floor(x / y) / z) [1] = floor((x /

Bit-wise operations to implement logical shift to the right [duplicate]

扶醉桌前 提交于 2019-12-20 07:40:26
问题 This question already has answers here : Implementing Logical Right Shift in C (8 answers) Closed 11 months ago . So I am trying to solve this home assignment and I have been stuck with this one particular problem for a couple of hours and can't figure it out. I feel like I am so close! But then i change something in the code and something else isn't right.. /* * logicalShift - shift x to the right by n, using a logical shift * Can assume that 0 <= n <= 31 * Examples: logicalShift(0x87654321