Integers
Integers
(OP)
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?
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS Contact USThanks. We have received your request and will respond promptly. Come Join Us!Are you a
Computer / IT professional? Join Tek-Tips Forums!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail. Posting Guidelines |
|
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Register now while it's still free!
Already a member? Close this window and log in.
RE: Integers
1001001000101010 = -4650 in qbasic
the first 1 means that it is negative, you could use it to make a larger number 37418.
RE: Integers
RE: Integers
Anyway.
Suggestion to your problem:
you want to store things 16 bit but work 0..65535?
Then you can store them Integer or String *2 and wrote a pair of functions the would convert them to (and from) 0..65535 LONG.
Of cource it would made your program slower (not sure qbasic have enough power to write NES emulator in it, anyway)
RE: Integers
I happened to just now be reading a document from the QuickBasic Knowledge Base that covers this stuff and have posted a small portion of it for you here since I can't dig up a url for it at the moment. You can get the entire QB KB (freeware) at my site if you'd like to have it. It's helped me find answers to many QB questions.
- Dav
http://www.thecodepost.com
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
RE: Integers