integer-overflow

How can I detect integer overflow on 32 bits int?

狂风中的少年 提交于 2019-11-28 23:09:55
I know such topic was asked several times, but my question is about overflow on full 32 bits of int . For example: 11111111111111111111111111111111 + 00000000000000000000000000000001 = 00000000000000000000000000000000 //overflow! I found topic with similar question about this, however the algorithm is not perfect. 11111111111111111111111111111111 + 00000000000000000000000000000000 = 00000000000000000000000000000000 //overflow! Is there any simple and fast way to check this ? Since Java 8 there is a set of methods in the Math class: toIntExact (long), addExact (int,int), subtractExact (int,int)

If a 32-bit integer overflows, can we use a 40-bit structure instead of a 64-bit long one?

纵然是瞬间 提交于 2019-11-28 15:56:45
If, say, a 32-bit integer is overflowing, instead of upgrading int to long , can we make use of some 40-bit type if we need a range only within 2 40 , so that we save 24 (64-40) bits for every integer? If so, how? I have to deal with billions and space is a bigger constraint. Damon Yes, but... It is certainly possible , but it is usually nonsensical (for any program that doesn't use billions of these numbers): #include <stdint.h> // don't want to rely on something like long long struct bad_idea { uint64_t var : 40; }; Here, var will indeed have a width of 40 bits at the expense of much less

Convert INT_MAX to float and then back to integer.

旧时模样 提交于 2019-11-28 11:17:10
In C programming, I find a weird problem, which counters my intuition. When I declare a integer as the INT_MAX ( 2147483647 , defined in the limits.h) and implicitly convert it to a float value, it works fine, i.e., the float value is same with the maximum integer. And then, I convert the float back to an integer, something interesting happens. The new integer becomes the minimum integer ( -2147483648 ). The source codes look as below: int a = INT_MAX; float b = a; // b is correct int a_new = b; // a_new becomes INT_MIN I am not sure what happens when the float number b is converted to the

Difference between two large numbers C#

泪湿孤枕 提交于 2019-11-28 08:17:03
问题 There are already solutions to this problem for small numbers : Here: Difference between 2 numbers Here: C# function to find the delta of two numbers Here: How can I find the difference between 2 values in C#? I'll summarise the answer to them all: Math.Abs(a - b) The problem is when the numbers are large this gives the wrong answer (by means of an overflow). Worse still, if (a - b) = Int32.MinValue then Math.Abs crashes with an exception (because Int32.MaxValue = Int32.MinValue - 1 ): System

What happens when auto_increment on integer column reaches the max_value in databases?

风格不统一 提交于 2019-11-28 04:27:30
I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value. But what happens when I get more than 2 (or 4) billion posts and integer is not enough? Is the integer overflowed and continues or is an exception thrown that I can handle? Yes, I could change to long as datatype, but how do I check when that is needed? And I think there is problem with getting the last_inserted_id()-functions if I use long as datatype for the ID-column. Jim Martin's

Unexpected results when working with very big integers on interpreted languages

断了今生、忘了曾经 提交于 2019-11-28 02:51:11
I am trying to get the sum of 1 + 2 + ... + 1000000000 , but I'm getting funny results in PHP and Node.js . PHP $sum = 0; for($i = 0; $i <= 1000000000 ; $i++) { $sum += $i; } printf("%s", number_format($sum, 0, "", "")); // 500000000067108992 Node.js var sum = 0; for (i = 0; i <= 1000000000; i++) { sum += i ; } console.log(sum); // 500000000067109000 The correct answer can be calculated using 1 + 2 + ... + n = n(n+1)/2 Correct answer = 500000000500000000 , so I decided to try another language. GO var sum , i int64 for i = 0 ; i <= 1000000000; i++ { sum += i } fmt.Println(sum) //

Simulating integer overflow in Python

最后都变了- 提交于 2019-11-28 01:56:14
Python 2 has two integer datatypes int and long , and automatically converts between them as necessary, especially in order to avoid integer overflow. I am simulating a C function in Python and am wondering if there are standard ways to re-enable integer overflow. For the nonce, I've used overflow_point = maxint + 1 if value > overflow_point: value -= 2 * overflow_point Is there a more standard way to do the same thing? I think the basic idea is sound, but needs some tweaks: your function doesn't overflow on sys.maxint+1 , but it should; sys.maxint can be exceeded several times over as a

BCrypt says long, similar passwords are equivalent - problem with me, the gem, or the field of cryptography?

前提是你 提交于 2019-11-28 01:31:28
问题 I've been experimenting with BCrypt, and found the following. If it matters, I'm running ruby 1.9.2dev (2010-04-30 trunk 27557) [i686-linux] require 'bcrypt' # bcrypt-ruby gem, version 2.1.2 @long_string_1 = 'f287ed6548e91475d06688b481ae8612fa060b2d402fdde8f79b7d0181d6a27d8feede46b833ecd9633b10824259ebac13b077efb7c24563fce0000670834215' @long_string_2 = 'f6ebeea9b99bcae4340670360674482773a12fd5ef5e94c7db0a42800813d2587063b70660294736fded10217d80ce7d3b27c568a1237e2ca1fecbf40be5eab8' def salted

Is the size of an array constrained by the upper limit of int (2147483647)?

不羁的心 提交于 2019-11-28 01:21:19
I'm doing some Project Euler exercises and I've run into a scenario where I have want arrays which are larger than 2,147,483,647 (the upper limit of int in C#). Sure these are large arrays, but for instance, I can't do this // fails bool[] BigArray = new BigArray[2147483648]; // also fails, cannot convert uint to int ArrayList BigArrayList = new ArrayList(2147483648); So, can I have bigger arrays? EDIT: It was for a Sieve of Atkin , you know, so I just wanted a really big one :D Anytime you are working with an array this big, you should probably try to find a better solution to the problem.

C++ Template for safe integer casts

有些话、适合烂在心里 提交于 2019-11-27 22:22:01
I am trying to write a C++ template function that will throw a runtime exception on integer overflow in casts between different integral types, with different widths, and possible signed/unsigned mismatch. For these purposes I'm not concerned with casting from floating-point types to integral types, nor other object-to-object conversions. I'd like to do this without having to write lots of special case code. This is what I currently have: template< typename T, typename R > void safe_cast( const T& source, R& result ) { // get the maximum safe value of type R R rMax = (R) ~0; if ( rMax < 0 ) //