what is the value if u add a value more than the maximum value for the data type
-
hi, may i know what suppose to be the answer for the below code and its reason for it? Int16 minValue=-32768; Int16 maxValue=32767; Int16 delta=2; Int16 Value = (Int16)(minValue - delta) Int16 Value = (Int16)(maxValue + delta) Thanks in advance.
-
it is not my homework, just something that i encounter while programming...i want to know how the adding and minus works....how can it be negative number when the delta is 1 and it is added to the max value?
ok, this is because the numbers are just a bunch of bits in memory (16 in your case) and the negative numbers are those starting (or ending ;) with 1 - for example: (100101....) - so if you add a value and the result is bigger the max pos value the last 1 will be set and alas the value will be negative.
-
hi, may i know what suppose to be the answer for the below code and its reason for it? Int16 minValue=-32768; Int16 maxValue=32767; Int16 delta=2; Int16 Value = (Int16)(minValue - delta) Int16 Value = (Int16)(maxValue + delta) Thanks in advance.
Hi, for most (if not all) data types MaxValue+1 and MinValue have the same bit representation. If your code is not detecting overflows, incrementing MaxValue will just result in Minvalue; if your code is organized to detect overflow, an OverflowException will be thrown. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Sorry for any delays in replying, I currently don't always get e-mail notifications.