integer-overflow

C integer overflow behaviour when assigning to larger-width integers

天大地大妈咪最大 提交于 2019-11-30 20:35:49
If I execute the following code in C: #include <stdint.h> uint16_t a = 4000; uint16_t b = 8000; int32_t c = a - b; printf("%d", c); It correctly prints '-4000' as the result. However, I'm a little confused: shouldn't there be an arithmetic overflow when subtracting a larger unsigned integer from the other? What casting rules are at play here? This question seems a bit noobish, so any references would be greatly appreciated. TrayMan The issue is actually somewhat complicated. Operands of arithmetic expressions are converted using specific rules that you can see in Section 3.2.1.5 of the

Unsigned arithmetic and integer overflow

拟墨画扇 提交于 2019-11-30 20:26:25
问题 I am trying to understand arithmetic overflow. Suppose I have the following, unsigned long long x; unsigned int y, z; x = y*z; y*z can lead to an integer overflow. Does casting one of the operands to an unsigned long long alleviate this issue. What is the expected result of the multiplication of a 64-bit operand with a 32-bit operand? 回答1: unsigned long long x; unsigned int y, z; x = y*z; The evaluation of the expression y*z is not affected by the context in which it appears. It multiplies

Why is (18446744073709551615 == -1) true?

假装没事ソ 提交于 2019-11-30 17:48:25
When I was working on string::npos I noticed something and I couldn't find any explanation for it on the web. (string::npos == ULONG_MAX) and (string::npos == -1) are true. So I tried this: (18446744073709551615 == -1) which is also true. How can it be possible? Is it because of binary conversation? Evan Carroll 18,446,744,073,709,551,615 This number mentioned, 18,446,744,073,709,551,615 , is actually 2^64 − 1 . The important thing here is that 2^64-1 is essentially 0-based 2^64 . The first digit of an unsigned integer is 0 , not 1 . So if the maximum value is 1 , it has two possible values: 0

What happens when a char is assigned a value too large to fit in a byte?

扶醉桌前 提交于 2019-11-30 15:48:54
问题 Let's say I have a char pointer: char * cp; int val2 = 2; cp = &val2; *cp = 1234; What will happen since the value 1234 is too large to fit in 1 byte? Will it just overflow and cause incorrect values to be stored, or will it somehow store the correct value across several bytes? 回答1: In *cp = 1234; , *cp refers to just one byte, the first (lowest-addressed) byte of val2 . In an assignment, the value of the right-hand side is converted to the type of the left side. So, 1234 will be converted to

Can a non-empty string have a hashcode of zero?

泪湿孤枕 提交于 2019-11-30 10:55:14
By "non-empty", I mean in this question a string which contains at least one non-zero character. For reference, here's the hashCode implementation : 1493 public int hashCode() { 1494 int h = hash; 1495 if (h == 0) { 1496 int off = offset; 1497 char val[] = value; 1498 int len = count; 1499 1500 for (int i = 0; i < len; i++) { 1501 h = 31*h + val[off++]; 1502 } 1503 hash = h; 1504 } 1505 return h; 1506 } and the algorithm is specified in the documentation. Before an integer overflow occurs, the answer is easy: it's no. But what I'd like to know is if, due to integer overflow, it's possible for

Can XOR of two integers go out of bounds?

这一生的挚爱 提交于 2019-11-30 04:10:44
I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The result is 5 . My question is - supposedly the integers (getting generated by the XOR operation) are too large due to this operation: LonelyInteger ^ arr[i] Which leads to a potentially large integer which cannot be represented by the datatype say int in this case. My questions are: Is it even possible that XOR will generate such a large integer value that

C integer overflow behaviour when assigning to larger-width integers

霸气de小男生 提交于 2019-11-30 04:04:46
问题 If I execute the following code in C: #include <stdint.h> uint16_t a = 4000; uint16_t b = 8000; int32_t c = a - b; printf("%d", c); It correctly prints '-4000' as the result. However, I'm a little confused: shouldn't there be an arithmetic overflow when subtracting a larger unsigned integer from the other? What casting rules are at play here? This question seems a bit noobish, so any references would be greatly appreciated. 回答1: The issue is actually somewhat complicated. Operands of

Is there some meaningful statistical data to justify keeping signed integer arithmetic overflow undefined?

坚强是说给别人听的谎言 提交于 2019-11-30 03:13:02
The C Standard explicitly specifies signed integer overflow as having undefined behavior . Yet most CPUs implement signed arithmetics with defined semantics for overflow (except maybe for division overflow: x / 0 and INT_MIN / -1 ). Compilers writers have been taking advantage of the undefinedness of such overflows to add more aggressive optimisations that tend to break legacy code in very subtle ways. For example this code may have worked on older compilers but does not anymore on current versions of gcc and clang : /* Tncrement a by a value in 0..255, clamp a to positive integers. The code

Does integer overflow cause undefined behavior because of memory corruption?

我的未来我决定 提交于 2019-11-30 02:40:30
I recently read that signed integer overflow in C and C++ causes undefined behavior: If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. I am currently trying to understand the reason of the undefined behavior here. I thought undefined behavior occurs here because the integer starts manipulating the memory around itself when it gets too big to fit the underlying type. So I decided to write a little test program in Visual Studio 2015 to test that theory with the following code:

Why is (18446744073709551615 == -1) true?

懵懂的女人 提交于 2019-11-30 01:40:27
问题 When I was working on string::npos I noticed something and I couldn't find any explanation for it on the web. (string::npos == ULONG_MAX) and (string::npos == -1) are true. So I tried this: (18446744073709551615 == -1) which is also true. How can it be possible? Is it because of binary conversation? 回答1: 18,446,744,073,709,551,615 This number mentioned, 18,446,744,073,709,551,615 , is actually 2^64 − 1 . The important thing here is that 2^64-1 is essentially 0-based 2^64 . The first digit of