User Tools

Site Tools


handling_20unsigned_2032-bit_20values

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:

        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. 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
handling_20unsigned_2032-bit_20values.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1