handling_20unsigned_2032-bit_20values

*by Richard Russell, October 2006*

BBC BASIC's 32-bit integers are all signed, i.e. they represent values between -2147483648 (&80000000) and +2147483647 (&7FFFFFFF); values outside this range can only be represented by floating-point numbers. Occasionally you may want to treat a 32-bit integer as if it was unsigned, i.e. representing a value between zero (&00000000) and +4294967295 (&FFFFFFFF), for example this could be necessary when interpreting a UINT value returned from a Windows API function.

The function listed below performs the necessary conversion. It takes as a parameter a normal 32-bit integer and returns the equivalent unsigned value:

DEF FNuint(N%) = (N% >>> 1)*2 + (N% AND 1)

This function is equivalent, but it is slightly slower and somewhat bigger than the previous one:

DEF FNuint(N%) = (N% AND &7FFFFFFF) - (N% < 0)*2147483648

The function below performs the reverse conversion, that is it takes an unsigned value (0 ⇐ N < 2^32) and converts it to the equivalent 32-bit integer:

DEF FNintu(N) = ((N / 2) << 1) - (INT(N / 2) <> N / 2)

Alternatively:

DEF FNintu(N) IF N > &7FFFFFFF THEN = N - 2^32 ELSE = N

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

handling_20unsigned_2032-bit_20values.txt · Last modified: 2018/04/17 16:31 by tbest3112

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International