Microsoft KB Archive/61434

= XOR, OR, AND Convert Floating Point to Integer =

Article ID: 61434

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0

-



This article was previously published under Q61434



SUMMARY
The Basic bitwise operators (AND, OR, XOR, and NOT) do not operate directly on floating-point numbers (SINGLE ! or DOUBLE #) or string variables ($). They also do not operate directly on currency data type numbers (@) in Microsoft Basic Professional Development System (PDS) version 7.00. When a bitwise operation is attempted with a floating-point variable that has a value greater than the maximum LONG integer (2,147,483,647), an &quot;Overflow&quot; error occurs.

To properly perform a bitwise operation on a floating-point variable, the floating-point number must be moved into a LONG using a bitwise move. Two functions are listed below to move between SINGLEs and LONGs so that bitwise operations can be used on SINGLE floating-point numbers.

This information applies to Microsoft QuickBasic versions 4.00, 4.00b, and 4.50, and Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS and MS OS/2, and Microsoft Basic PDS version 7.00 for MS-DOS and MS OS/2.



MORE INFORMATION
When a bitwise operation is attempted on a floating point or currency numeric type, the fractional number is converted to its INTEGER or LONG equivalent and the operation is then performed using INTEGERs or LONGs. This can cause an &quot;Overflow&quot; error message if the value of the floating-point number exceeds the maximum value of a LONG integer.

Another method of performing bitwise operations on a floating-point number is to convert the number to a LONG, manipulate the bits, and then convert it back to a floating-point number. The code example below demonstrates moving between SINGLEs and LONGs using the MKS$, MKL$, CVS, and CVL functions. Converting DOUBLEs is more involved because they are longer than any integer type. A similar operation could be performed to convert DOUBLEs using two LONGs and the MKD$ and CVD functions as well.

Code Example
The following code example performs a bitwise move by using the MKS$, MKL$, CVS, and CVL functions using a string as an intermediary: 'Module level demonstrates using functions s! = 1.0 t! = 1.1 l& = SingleToLong(s!) m& = SingleToLong(t!) n& = l& XOR m&                   'Bit AND (won't work with SINGLEs) PRINT HEX$(l&), HEX$(m&), HEX$(n&)'3F800000  3F8CCCCD   3F800000 u! = LongToSingle(n&)            'Equivalent of bit AND of s! and 't! PRINT u!                         '1.75495E-39 END

FUNCTION SingleToLong&(s!) SingleToLong& = CVL(MKS$(s!)) END FUNCTION

FUNCTION LongToSingle!(l&) LongToSingle! = CVS(MKL$(l&)) END FUNCTION

Additional query words: QuickBas BasicCom

Keywords: KB61434

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.