This shows you the differences between two versions of the page.
handling_20unsigned_2032-bit_20values [2018/03/31 14:19] 127.0.0.1 external edit |
handling_20unsigned_2032-bit_20values [2018/04/17 17:31] (current) tbest3112 Added syntax highlighting |
||
---|---|---|---|
Line 2: | Line 2: | ||
//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:\\ | //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 bb4w> | ||
DEF FNuint(N%) = (N% >>> 1)*2 + (N% AND 1) | 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:\\ | This function is equivalent, but it is slightly slower and somewhat bigger than the previous one:\\ | ||
+ | <code bb4w> | ||
DEF FNuint(N%) = (N% AND &7FFFFFFF) - (N% < 0)*2147483648 | 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:\\ | \\ 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 bb4w> | ||
DEF FNintu(N) = ((N / 2) << 1) - (INT(N / 2) <> N / 2) | DEF FNintu(N) = ((N / 2) << 1) - (INT(N / 2) <> N / 2) | ||
+ | </code> | ||
Alternatively:\\ | Alternatively:\\ | ||
+ | <code bb4w> | ||
DEF FNintu(N) IF N > &7FFFFFFF THEN = N - 2^32 ELSE = N | DEF FNintu(N) IF N > &7FFFFFFF THEN = N - 2^32 ELSE = N | ||
+ | </code> |