User Tools

Site Tools


handling_20unsigned_2032-bit_20values

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
handling_20unsigned_2032-bit_20values [2018/03/31 13:19] – external edit 127.0.0.1handling_20unsigned_2032-bit_20values [2024/01/05 00:22] (current) – external edit 127.0.0.1
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>
handling_20unsigned_2032-bit_20values.1522502363.txt.gz · Last modified: 2024/01/05 00:17 (external edit)