KenshinHimura
Programmer
Is there any possible way to get unsigned integer in qbasic. Or basically any other data type that is exactly 16-bits without negative numbers?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Here is the simplest function to convert a 2 byte unsigned integer
(stored in the Basic variable num%) to an unsigned number stored in a
Basic LONG integer:
num% = -1
PRINT unsignedlong&(num%) ' Prints 65535
FUNCTION unsignedlong& (num%)
IF num% < 0 THEN
unsignedlong& = num% + 65536
ELSE
unsignedlong& = num%
END IF
END FUNCTION
You can also convert an unsigned integer to a positive Basic LONG
integer using bit manipulation as follows:
1. Check if the integer is positive or zero. If Basic already
recognizes the number as positive or zero, then either use it as
is, or assign it directly to a long integer and skip the next three
steps.
2. Otherwise, if the number (x%) is negative, then set the high bit to
zero, as follows:
x% = x% AND &H7FFF&
3. Assign the number to a long integer, as follows:
y& = x%
4. Set the 15th bit (counting from bit zero) back to a one, as
follows:
y& = (y& OR &H8000&)
The long integer (y&) now contains the correct positive integer
that the other-language routine meant to pass back to Basic.
Here is the simplest bit manipulation function to convert an unsigned
integer to a positive Basic LONG integer:
num% = -1
PRINT Unsigned&(num%) ' Prints 65535
FUNCTION Unsigned&(param%)
Unsigned& = &HFFFF& AND param%
END FUNCTION
The following program converts the unsigned integer stored in x% to a
positive LONG integer stored in y&:
x% = -1 ' -1 in two's complement is 65535 as unsigned integer
IF x% < 0 THEN
' Set the 15th bit to zero (counting from bit 0):
x% = (x% AND &H7FFF&)
' Assign it to a LONG integer:
y& = x%
' Set the 15th bit back to a one:
y& = (y& OR &H8000&)
ELSE
y& = x%
END IF
PRINT y& ' Prints 65535