Arithmetic overflow handling

New to FreeBASIC? Post your questions here.
fzabkar
Posts: 6
Joined: Sep 29, 2018 2:52

Arithmetic overflow handling

Postby fzabkar » Oct 08, 2019 20:52

If I add two large 16-bit numbers in FreeBASIC, and if the result is a 17-bit number, then the 17th bit goes into the bit bucket without generating an error message. This is the behaviour that I prefer when I am computing a checksum over a word array.

I am currently maintaining source code written in Visual Basic 6. The author has written a checksum calculation routine which accounts for overflows. He does this by using a 32-bit variable to store the result of each 16-bit addition, and then subtracts 0x10000 from the 32-bit variable whenever there is a 17-bit result.

Does VB6 really differ from FreeBASIC in this respect (overflow handling), or is the author being too careful?
badidea
Posts: 1542
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Arithmetic overflow handling

Postby badidea » Oct 08, 2019 21:12

Depends on the application, I would say.
For certain applications it might not be important that an overflow occurs.
If you are sure that your inputs can never cause an overflow, then no problem.
If a bank uses 16-bit numbers for the amount deposited, some would not be happy (others would).
Also, if you detect overflow, what do you do with it? print a warning/error? change your code?

Simple test with unsigned 16-bit:

Code: Select all

dim as ushort a, b
dim as ulong c
a = &hE000
b = &hF000
c = a + b
print hex(a), hex(b), hex(c)
if c and &hffff0000 then
   print "overflow"
   c -= &h10000 'OR: c and= &hffff
   print hex(a), hex(b), hex(c)
end if

Return to “Beginners”

Who is online

Users browsing this forum: Baidu [Spider] and 19 guests