Handling+unsigned+32-bit+values

//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: code format="bb4w" DEF FNuint(N%) = (N% >>> 1)*2 + (N% AND 1) code This function is equivalent, but it is slightly slower and somewhat bigger than the previous one: code format="bb4w" DEF FNuint(N%) = (N% AND &7FFFFFFF) - (N% < 0)*2147483648 code

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: code format="bb4w" DEF FNintu(N) = ((N / 2) << 1) - (INT(N / 2) <> N / 2) code Alternatively: code format="bb4w" DEF FNintu(N) IF N > &7FFFFFFF THEN = N - 2^32 ELSE = N code