integer-overflow

How to simulate 32-bit signed integer overflow in PL/SQL?

泄露秘密 提交于 2019-12-04 19:25:35
Do you know how to simulate a 32-bit integer overflow in Oracle PL/SQL. For instance, 2147483647 + 1 = -2147483648 or -2147483648 - 1 = 212147483647 I tried PLS_INTEGER, but it throws an overflow exception. Perhaps you could trap the overflow exception, as in the following: DECLARE n PLS_INTEGER; addend PLS_INTEGER; NUMERIC_OVERFLOW EXCEPTION; PRAGMA EXCEPTION_INIT(NUMERIC_OVERFLOW, -1426); BEGIN n := 2147483642; addend := 6; BEGIN n := n + addend; EXCEPTION WHEN NUMERIC_OVERFLOW THEN DBMS_OUTPUT.PUT_LINE('OVERFLOW!'); n := -2147483647 + (-2147483647 + n + addend - 1); WHEN OTHERS THEN DBMS

Delphi: How to avoid EIntOverflow underflow when subtracting?

大憨熊 提交于 2019-12-04 18:23:47
问题 Microsoft already says, in the documentation for GetTickCount, that you could never compare tick counts to check if an interval has passed. e.g.: Incorrect (pseudo-code): DWORD endTime = GetTickCount + 10000; //10 s from now ... if (GetTickCount > endTime) break; The above code is bad because it is suceptable to rollover of the tick counter. For example, assume that the clock is near the end of it's range: endTime = 0xfffffe00 + 10000 = 0x00002510; //9,488 decimal Then you perform your check:

double precision integer subtraction with 32-bit registers(MIPS)

强颜欢笑 提交于 2019-12-04 18:21:22
I am learning computer arithmetic. The book I use(Patterson and Hennessey) lists the below question. Write mips code to conduct double precision integer subtraction for 64-bit data. Assume the first operand to be in registers $t4(hi) and $t5(lo), second in $t6(hi) and $t7(lo). My solution to the answer is sub $t3, $t5, $t7 # Subtract lo parts of operands. t3 = t5 - t7 sltu $t2, $t5, $t7 # If the lo part of the 1st operand is less than the 2nd, # it means a borrow must be made from the hi part add $t6, $t6, $t2 # Simulate the borrow of the msb-of-low from lsb-of-high sub $t2, $t4, $t6 #

How to check if a number overflows an 'int' [duplicate]

痴心易碎 提交于 2019-12-04 11:36:02
This question already has answers here : Closed 6 years ago . Possible Duplicate: Best way to detect integer overflow in C/C++ I was asked this question in an interview : "Convert a string representation of a number into an integer". But if the number exceeds the max value that can be stored in a 32 bit integer, it must throw an error. My question is how can we check if a number overflows a 32-bit unsigned int ? sampson-chen (I looked at the possible duplicate to this question and I'm uncertain how useful that reading will be, since in an interview context, you'd be expected describe the

Should I use unsigned integers for counting members?

只谈情不闲聊 提交于 2019-12-04 08:35:19
问题 Should I use unsigned integers for my count class members? Answer For example, assume a class TList <T> = class private FCount : Cardinal; public property Count : Cardinal read FCount; end; That does make sense, doesn't it? The number of items stored in a list can't be negative, so why not use an unsigned integer type for it? I think it's in general a good principle to always use the least general (ergo the most special) type possible. Now, iterating over a list looks like this: for I := 0 to

Java multiply operation behavior

瘦欲@ 提交于 2019-12-04 04:52:35
I wrote a method to convert a given number from days to milliseconds: private long expireTimeInMilliseconds; ... public void setExpireTimeInDays(int expireTimeInDays) { expireTimeInMilliseconds = expireTimeInDays * 24 * 60 * 60 * 1000; } I had a hard time to figure out what I did wrong. Now my question: Is that error so obvious ? The corrected method: private long expireTimeInMilliseconds; ... public void setExpireTimeInDays(int expireTimeInDays) { expireTimeInMilliseconds = ((long) expireTimeInDays) * 24 * 60 * 60 * 1000; } If I don't convert the integer to long before calculating, I get a

Why does C# issue the error “cannot implicitly convert int to ushort” against modulo arithmetic on ushorts?

微笑、不失礼 提交于 2019-12-04 03:17:55
问题 In another thread, someone asked about why adding two ushort values raised errors in C#. e.g. ushort x = 4; ushort y = 23; ushort z = x+y; // ERROR cannot implicitly convert int to ushort On that thread, people argued that the plus + operater takes two ints by default, and this is a language feature to help avoid arithmetic overflows. But I get the same kind of error in the following function: public RGB(ushort red, ushort green, ushort blue) { // this class RGB has three ushort fields: r, g,

How does one safely static_cast between unsigned int and int?

a 夏天 提交于 2019-12-04 01:50:32
I have an 8-character string representing a hexadecimal number and I need to convert it to an int . This conversion has to preserve the bit pattern for strings "80000000" and higher, i.e., those numbers should come out negative. Unfortunately, the naive solution: int hex_str_to_int(const string hexStr) { stringstream strm; strm << hex << hexStr; unsigned int val = 0; strm >> val; return static_cast<int>(val); } doesn't work for my compiler if val > MAX_INT (the returned value is 0). Changing the type of val to int also results in a 0 for the larger numbers. I've tried several different

Is this a JVM bug or “expected behavior”?

﹥>﹥吖頭↗ 提交于 2019-12-03 18:22:41
问题 I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering if something if there is a bug in the JVM or if perhaps this is a fringe case where I don't understand some of the details of what exactly is supposed to happen. Suppose we had the following code in a main method by itself: int i; int count = 0; for(i=0; i < Integer.MAX_VALUE; i+=2){ count++; } System.out.println(i++); A naive expectation would be that this would print Integer.MAX_VALUE-1 ,

detecting multiplication of uint64_t integers overflow with C

大兔子大兔子 提交于 2019-12-03 16:51:57
问题 Is there any efficient and portable way to check when multiplication operations with int64_t or uint64_t operands overflow in C? For instance, for addition of uint64_t I can do: if (UINT64_MAX - a < b) overflow_detected(); else sum = a + b; But I can not get to a similar simple expression for multiplication. All that occurs to me is breaking the operands into high and low uint32_t parts and performing the multiplication of those parts while checking for overflow, something really ugly and