I have a latitude value as double and I want to perform a Bitwise AND operation on it followed by right shift of bits. Following is my line of code:
pBuffer[1]=(latitude_decimal_degrees & 0xFF0000) >> 16;
However a Bitwise AND operation between a double and int value is not possible. I cannot convert the latitude to int as this would not yield an accurate value. Can anybody guide me in this?
EDIT: My requirement is basically to translate the following vb.net code into java. Reason: The lines of code below (vb.net) is part of a method written in "Basic4Android" for an android app. The exact same method is to be implement in my BlackBerry App. Therefore I need to have the exact same value generated as below which will be decoded at the server end:
Dim pBuffer(11) As Int
Dim longitude_decimal_degrees As Double
pBuffer(1)=Bit.ShiftRight(Bit.And(latitude_decimal_degrees, 0xFF0000),16)
How can these lines of code be translated into java?
You can turn the double into a long via bits using Double.doubleToRawLongBits(latitude_decimal_degrees) perform all bitwise operations in the long space, then convert back to a double via longBitsToDouble
See also this SO answer: https://stackoverflow.com/a/4211117/504685
As you yourself, and others have noted, its not possible to perform bit shifting on floating point numbers. Now, based on your updated question you are using a custom library that implements its own version of bit operators. All operands to these operators are converted to int's
ShiftRight (N As Int, Shift As Int) As Int
And (N1 As Int, N2 As Int) As Int
In order to match this logic, your Java code should also cast its double value to an int before performing the necessary bit operations:
double latitude = 52.5233;
int transform = (((int)latitude) & 0xFF000000) >> 16;
Note that this assumes that Basic4Android follows the same casting rule from int to double as Java (numbers are rounded down to whole).
When you are done porting the code over pass a battery of values through it and make sure the end result is the same in both your Basic4Android and Java code.