integer-overflow

Allowing signed integer overflows in C/C++

▼魔方 西西 提交于 2019-11-26 21:06:05
I want signed integers to overflow when they become too big. How do I achieve that without using the next biggest datatype (or when I am already at int128_t)? For example using 8bit integers 19*12 is commonly 260, but I want the result 1 11 10 01 00 with the 9th bit cut off, thus -27. Signed overflow is undefined in C, and that's for real . One solution follows: signed_result = (unsigned int)one_argument + (unsigned int)other_argument; The above solution involves implementation-defined behavior in the final conversion from unsigned to int but do not invoke undefined behavior. With most

C++ Template for safe integer casts

左心房为你撑大大i 提交于 2019-11-26 20:57:45
问题 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(

How disastrous is integer overflow in C++?

巧了我就是萌 提交于 2019-11-26 20:34:41
问题 I was just wondering how disastrous integer overflow really is. Take the following example program: #include <iostream> int main() { int a = 46341; int b = a * a; std::cout << "hello world\n"; } Since a * a overflows on 32 bit platforms, and integer overflow triggers undefined behavior, do I have any guarantees at all that hello world will actually appear on my screen? I removed the "signed" part from my question based on the following standard quotes: (§5/5 C++03, §5/4 C++11) If during the

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

半腔热情 提交于 2019-11-26 19:22:00
问题 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

Addition of two chars produces int

眉间皱痕 提交于 2019-11-26 18:58:28
I've made a simple program and compiled it with GCC 4.4/4.5 as follows: int main () { char u = 10; char x = 'x'; char i = u + x; return 0; } g++ -c -Wconversion a.cpp And I've got the following: a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to ‘char’ from ‘int’ may alter its value The same warning I've got for the following code: unsigned short u = 10; unsigned short x = 0; unsigned short i = u + x; a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to ‘short unsigned int’ from ‘int’ may alter its value Could anyone please explain me why addition of two chars

Why, In Java arithmetic, overflow or underflow will never throw an Exception?

一曲冷凌霜 提交于 2019-11-26 17:48:20
问题 During Java Arithmetic operation , JVM do not throw Underflow or Overflow Exception. So many time we come across unexpected results and wondering what went wrong. While in case of .NET technology we have Overflow and Undeflow exception. So my question is that , why Java was design not to throw this exception during arithmetic operation 回答1: This was likely a combination of factors: The big languages prior to Java used unchecked arithmetic. Well-known algorithms prone to numerical overflow

Overflow when multiplying Integers and assigning to Long

社会主义新天地 提交于 2019-11-26 17:23:24
问题 If I type the following into the immediate window I get Runtime error '6': Overflow. MsgBox 24 * 60 * 60 Why is this? This also fails: Dim giveTime As Long giveTime = 24 * 60 * 60 Why is this? giveTime is declared as a Long type, so 24 × 60 × 60 = 86400 should comfortably fit. 回答1: This is a really odd VBA quirk. I'm amazed I've never bumped into this. Dim x As Long x = 24 * 60 * 60 ' Overflow x = 32767 + 1 ' Overflow. x = 32768 + 1 ' Works fine! So it looks like the * and + operators return

Force PHP integer overflow

荒凉一梦 提交于 2019-11-26 16:41:31
We have some integer arithmetic which for historical reasons has to work the same on PHP as it does in a few statically typed languages. Since we last upgraded PHP the behavior for overflowing integers has changed. Basically we are using following formula: function f($x1, $x2, $x3, $x4) { return (($x1 + $x2) ^ $x3) + $x4; } However, even with conversions: function f($x1, $x2, $x3, $x4) { return intval(intval(intval($x1 + $x2) ^ $x3) + $x4); } I am still ending up with the completely wrong number... For example, with $x1 = -1580033017, $x2 = -2072974554, $x3 = -1170476976) and $x4 = -1007518822

What does BigInteger having no limit mean?

大城市里の小女人 提交于 2019-11-26 16:36:48
I looked into this stackoverflow question relating to Big Integer and specifically I do not understand this line (the words in italics): In the BigInteger class, I have no limits and there are some helpful functions there but it is pretty depressing to convert your beautiful code to work with the BigInteger class, specially when primitive operators don't work there and you must use functions from this class. I don't know what I am missing but to represent something that has no limit you would require infinite memory ? Whats is the trick here ? There is no theoretical limit. The BigInteger

Question about C behaviour for unsigned integer underflow

天涯浪子 提交于 2019-11-26 15:32:01
I have read in many places that unsigned integer overflow is well-defined in C unlike the signed counterpart. Is underflow the same? For example: unsigned int x = -1; // Does x == UINT_MAX? Thanks. I can't recall where, but i read somewhere that arithmetic on unsigned integral types is modular, so if that were the case then -1 == UINT_MAX mod (UINT_MAX+1). Stephen Canon §6.2.5, paragraph 9: A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the