User Tools

Site Tools


modulo_20integer_20arithmetic

Modulo integer arithmetic

by Richard Russell, July 2007

(Note: In later versions of BBC BASIC for Windows and BBC BASIC for SDL 2.0 the SUM operator provides modulo 64-bit addition: a = b SUM c).

Some implementations of BBC BASIC (for example Acorn versions) allow you to add (or subtract) two 32-bit integers in such a way that the result 'wraps around', i.e. is evaluated modulo 2^32. For example in such versions the following code prints the hexadecimal value 80000003:

        B% = &40000001
        C% = &40000002
        A% = B% + C%
        PRINT ~A%

However in BBC BASIC for Windows this code results in the Number too big error because it attempts to load A% with a value outside the valid range of an integer variable (-2147483648 to +2147483647).

Generally the BBC BASIC for Windows behaviour is more consistent, and is less likely to give rise to unexpected results and potential program bugs. However on rare occasions the 'modulo' behaviour can actually be useful.

If you want to reproduce the modulo arithmetic behaviour with BBC BASIC for Windows you can use code similar to the following:

        B% = &40000001
        C% = &40000002
        A% = FN32(B% + C%)
        PRINT ~A%

where FN32 is defined as follows:

        DEF FN32(V)
        WHILE V>&7FFFFFFF : V-=2^32 : ENDWHILE
        WHILE V<&80000000 : V+=2^32 : ENDWHILE
        = V

This will work correctly so long as you are adding or subtracting only two values. If you need to add three or more values you can either run the program in *FLOAT 64 mode or split the expression up into operations on no more than two values, for example:

        A% = FN32(FN32(B% + C%) + FN32(D% + E%))

How it works

The floating-point variable V consists of a 32-bit mantissa and an 8-bit exponent. Superficially one might imagine that, when containing an integer value, it can represent only the same numeric range as a 32-bit integer variable; if that were the case the code in FN32() would not work, since the numeric overflow would result in a loss of accuracy.

However because of the way (40-bit) floating-point numbers are represented they can in fact contain, without any loss of precision, the equivalent of a 33-bit integer, that is any integer value from -4294967296 TO +4294967295. Thus they can contain, without overflow or truncation, the sum or difference of any two 32-bit integers; therefore the reduction to a valid 32-bit range using the code in FN32() can take place accurately.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
modulo_20integer_20arithmetic.txt · Last modified: 2024/01/05 00:21 by 127.0.0.1