integer-arithmetic

Determining if a number is either a multiple of ten or within a particular set of ranges

梦想的初衷 提交于 2019-12-02 16:54:11
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 } else { do this } //this part is for 1-10, 21-30, 41-50, 61-70, 81-90 This is for a snakes and ladders board game, if it makes any more sense for my question. I imagine the first if statement I'll need to use modulus, would if (num == 100%10) be correct? The second one I have no idea. I can write it out like if (num > 10 && num is < 21 || etc) but

Is masking before unsigned left shift in C/C++ too paranoid?

陌路散爱 提交于 2019-12-02 15:42:08
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 this: b = (a << 31) & 0xFFFFFFFF where a and b are unsigned 32-bit integers. Notice that in the result, we discard any bits above the least significant 32 bits. As a first naive approximation, we might assume that int is 32 bits wide on most platforms, so we would write: unsigned int a = (...); unsigned int b = a << 31; We know this code won't work

Check of overflow in signed addition and abelian groups

这一生的挚爱 提交于 2019-12-02 13:33:49
问题 I was reading about why the following code is buggy: int tadd_ok ( int x, int y ) { int sum = x + y; return ( sum - x == y ) && ( sum - y == x ); } The explanation was that two's complement addition forms an abelian group and so the expression (x + y) - x with evaluate to y regardless if whether or not the addition overflows. (Same for (x + y) - y) which will evaluate to x ). I don't understand this explanation or the abelian group reference. Two's complement addition is basically unsigned

Raising a number to a huge exponent

允我心安 提交于 2019-12-02 11:21:11
问题 I am given the number 3 and a variable 'n', that can be as high as 1 000 000 000 (a billion). I have to print the answer of 3^n modulo 100003 . I tried the following: I tried using the function std::pow(3,n) , but it doesn't work for large exponents(can't apply the modulo during the process). I tried implementing my own function that would raise the number 3 to the power n so I could apply the modulo when needed, but when tested with very large numbers, this method proved to be too slow.

Multiplication of two 16-bit numbers - Why is the result 32-bit long? [closed]

∥☆過路亽.° 提交于 2019-12-02 10:03:01
If I multiplie two 16-bit numbers, the result will be 32-bit long. But why is this so? What is the clear explanation for this? And for my right understanding: The calculation for this is: n-bit number multiplied with a m-bit number gives a (n+m) bit number? (2 n - 1)*(2 m - 1) = 2 n+m - 2 n - 2 m + 1 -(2 n + 2 m ) is like clearing the bits at index n and m , which does not affect much the result compared to 2 n+m , so you need n+m bits to represent the result. For example 1111 2 *1111 2 = 11100001 2 (15*15 = 225) In general, (b n - 1)*(b m - 1) = b n+m - b n - b m + 1 , so multiply an n-digit

Raising a number to a huge exponent

可紊 提交于 2019-12-02 07:03:20
I am given the number 3 and a variable 'n', that can be as high as 1 000 000 000 (a billion). I have to print the answer of 3^n modulo 100003 . I tried the following: I tried using the function std::pow(3,n) , but it doesn't work for large exponents(can't apply the modulo during the process). I tried implementing my own function that would raise the number 3 to the power n so I could apply the modulo when needed, but when tested with very large numbers, this method proved to be too slow. Lastly I tried prime factorization of the number 'n' and then using the factors of 'n' (and how many times

Is this an overflow arithmetic calculation?

孤人 提交于 2019-12-02 06:53:49
So when I read the book and it says that overflow can't occur when add different signs and subtraction of the same sign. But I have question when I do this: 185 - 122 I converted binary of 122 to 2s complement and did the addition, which is different signs: 185+(-122) and when I add them together, I got the sign bit overflow to 100111111. But if I cut off the MSB on the left, it is the correct answer. Is it an overflow? No, it isn't overflow - the overflow resulting from the addition of 2 1's in the MSB must just be discarded. From Wikipedia To get the two's complement of a binary number, the

Check of overflow in signed addition and abelian groups

▼魔方 西西 提交于 2019-12-02 05:40:51
I was reading about why the following code is buggy: int tadd_ok ( int x, int y ) { int sum = x + y; return ( sum - x == y ) && ( sum - y == x ); } The explanation was that two's complement addition forms an abelian group and so the expression (x + y) - x with evaluate to y regardless if whether or not the addition overflows. (Same for (x + y) - y) which will evaluate to x ). I don't understand this explanation or the abelian group reference. Two's complement addition is basically unsigned modulo arithmetic that is "converted" to two's complement, right? So for example if we have 4 bits we

Trick to divide a constant (power of two) by an integer

随声附和 提交于 2019-12-01 17:08:38
问题 NOTE This is a theoretical question. I'm happy with the performance of my actual code as it is. I'm just curious about whether there is an alternative. Is there a trick to do an integer division of a constant value, which is itself an integer power of two, by an integer variable value, without having to use do an actual divide operation? // The fixed value of the numerator #define SIGNAL_PULSE_COUNT 0x4000UL // The division that could use a neat trick. uint32_t signalToReferenceRatio(uint32_t

How to perform ceiling-division in integer arithmetic? [duplicate]

核能气质少年 提交于 2019-12-01 16:54:25
This question already has an answer here: Is there a ceiling equivalent of // operator in Python? 7 answers It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes. Is there a way to divide that rounds upwards if there is a non-zero remainder? For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division: items = 102 boxsize = 10 num_boxes = (items + boxsize - 1) // boxsize Alternatively, use negation to convert floor division to