integer-overflow

Integer Overflow - Why not [duplicate]

こ雲淡風輕ζ 提交于 2019-12-23 07:27:37
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Addition of two chars produces int Given the following C++ code: unsigned char a = 200; unsigned char b = 100; unsigned char c = (a + b) / 2; The output is 150 as logically expected, however shouldn't there be an integer overflow in the expression (a + b) ? Obviously there must be an integer promotion to deal with the overflow here, or something else is happening that I cannot see. I was wondering if someone

Are fixed-width integers distributive over multiplication?

不问归期 提交于 2019-12-23 06:53:33
问题 For three n-bit signed integers a , b , and c (such as 32-bit), is it always true that a * (b + c) == (a * b) + (a * c) , taking into account integer overflow? I think this is language-independent, but if it's not, I'm specifically interested in the answer for Java. 回答1: Yes, it holds, because integer arithmetic is modulo arithmetic over finite rings. You can see some theoretical discussion here: https://math.stackexchange.com/questions/27336/associativity-commutativity-and-distributivity-of

Are fixed-width integers distributive over multiplication?

回眸只為那壹抹淺笑 提交于 2019-12-23 06:52:06
问题 For three n-bit signed integers a , b , and c (such as 32-bit), is it always true that a * (b + c) == (a * b) + (a * c) , taking into account integer overflow? I think this is language-independent, but if it's not, I'm specifically interested in the answer for Java. 回答1: Yes, it holds, because integer arithmetic is modulo arithmetic over finite rings. You can see some theoretical discussion here: https://math.stackexchange.com/questions/27336/associativity-commutativity-and-distributivity-of

VB6 how to get C-like integer overflow

醉酒当歌 提交于 2019-12-23 04:28:10
问题 I want to port this simple hash algorithm to VB6. I have come up with: Public Function simpleHash(hashString As String) As Long Dim hash As Long Dim c As Long Dim i As Integer On Local Error Resume Next hash = 5381 For i = 1 To Len(hashString) c = AscW(Mid$(hashString, i, 1)) hash = hash * 33 + c Next i simpleHash = hash End Function The problem is, despite my On Error statement which suppresses the Error 6: Overflow exceptions, the variable hash is not updated any more if an overflow occurs.

C++ literal integer type

瘦欲@ 提交于 2019-12-22 18:31:55
问题 Do literal expressions have types too ? long long int a = 2147483647+1 ; long long int b = 2147483648+1 ; std::cout << a << ',' << b ; // -2147483648,2147483649 回答1: Yes, literal numbers have types. The type of an unsuffixed decimal integer literal is the first of int , long , long long in which the integer can be represented. The type of binary, hex and octal literals is selected similarly but with unsigned types in the list as well. You can force the use of unsigned types by using a U

what's the difference between mid=(beg+end)/2 and mid=beg+(end-beg)/2 in binary search?

空扰寡人 提交于 2019-12-22 04:07:41
问题 It is a problem from C++ primer fifth edition problem 3.26, I don't know the difference between them ? May be the second one can avoid overflow. 回答1: May be the second one can avoid overflow. Exactly. There's no guarantee that beg+end is representable; but in the second case the intermediate values, as well as the expected result, are no larger than end , so there is no danger of overflow. The second form can also be used for affine types like pointers and other random-access iterators, which

What is the right way to find the average of two values?

假装没事ソ 提交于 2019-12-21 07:17:21
问题 I recently learned that integer overflow is an undefined behavior in C (side question - is it also UB in C++?) Often in C programming you need to find the average of two values a and b . However doing (a+b)/2 can result in overflow and undefined behavior. So my question is - what is the right way to find the average of two values a and b in C? 回答1: With help from Secure Coding if (((si_b > 0) && (si_a > (INT_MAX - si_b))) || ((si_b < 0) && (si_a < (INT_MIN - si_b)))) { /* will overflow, so

Delphi: How do i use $OVERFLOWCHECKS OFF to disable overflow checks?

烈酒焚心 提交于 2019-12-21 05:06:09
问题 i have bit of code that causes an underflow: var t1, t2, delta: DWORD: begin t1 := 0xffffff00; t2 := 0x00000037; delta := (t2 - t1); The subtraction itself does generate an overflow (underflow), but i don't want Delphi to throw an EIntOverflow exception. So i try disabling the generation of overflow checking code by disabling overflow checking: var t1, t2, delta: DWORD: begin t1 := 0xffffff00; t2 := 0x00000037; {$OVERFLOWCHECKS OFF} delta := (t2 - t1); {$OVERFLOWCHECKS ON} Yet even with the

Bug in quicksort example (K&R C book)?

一个人想着一个人 提交于 2019-12-21 04:34:06
问题 This quicksort is supposed to sort "v[left]...v[right] into increasing order"; copied (without comments) from The C Programming Language by K&R (Second Edition): void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left >= right) return; swap(v, left, (left + right) / 2); last = left; for (i = left+1; i <= right; i++) if (v[i] < v[left]) swap(v, ++last, i); swap(v, left, last); qsort(v, left, last-1); qsort(v, last+1, right); } I think there's a bug at

How do I detect overflow while multiplying two 2's complement integers?

久未见 提交于 2019-12-21 03:18:20
问题 I want to multiply two numbers, and detect if there was an overflow. What is the simplest way to do that? 回答1: Multiplying two 32 bit numbers results in a 64 bit answer, two 8s give a 16, etc. binary multiplication is simply shifting and adding. so if you had say two 32 bit operands and bit 17 set in operand A and any of the bits above 15 or 16 set in operand b you will overflow a 32 bit result. bit 17 shifted left 16 is bit 33 added to a 32. So the question again is what are the size of your