0

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?

Perception
  • 77,470
  • 19
  • 176
  • 187
Sarah
  • 1,845
  • 2
  • 19
  • 39
  • 6
    It would be better if you described what you are trying to accomplish, rather than how you think you are going to accomplish it. – Perception Dec 18 '12 at 07:28
  • 1
    This looks like [an XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), indeed. – Joachim Sauer Dec 18 '12 at 07:30
  • 1
    @Perception is right: **what are you actually trying to do**? This is not possible, because it does not make *sense*. We do not get what you want to do, how should a compiler? Do you e.g. want to get the integer part of the double latitude? Then use `round` or `floor`. – Has QUIT--Anony-Mousse Dec 18 '12 at 07:37
  • @Perception I am trying to apply a particular format expected at the server end to send my values through to the server. The values (latitude) will be sent in hex format and will be encrypted. Bit shifts are applied for security purpose. Idea is to AND the latitude with 0xFF0000 and shift right the result by 16. The final value will then be encrypted and sent to the server. This is part of my BlackBerry application. – Sarah Dec 18 '12 at 07:43
  • 2
    If your `latitude` is a `double` value, and it would be possible to `AND` it with 0xFF0000 you would basically loose all the information and could not get it back. Are you aware of that? Can you describe the format expected at the server? – jlordo Dec 18 '12 at 07:46
  • @jlordo the server accepts binary data in hex encoded format. – Sarah Dec 18 '12 at 07:53
  • 1
    At the end of the day all the data is binary. I don't know what you mean by hex encoded. Binary and hexadecimal are systems to represent numbers. So every binary number can be transformed to a hex number. But a even a hex number is stored in binary on any computer. We know that you have the `latitude` as a `double` value, but in order for us to help you we need way more details about the format you are converting it to. – jlordo Dec 18 '12 at 07:57
  • @jlordo my basic query is how can I perform a Bitwise AND operation between a double and int value. Do I really need to provide more details? – Sarah Dec 18 '12 at 08:13
  • for that, see [@Charlie](http://stackoverflow.com/users/504685/charlie) answer below. But beware of potentially unexpected results like `NaN` or likewise. – jlordo Dec 18 '12 at 08:18
  • I'm not a VB expert, but I do believe that the code you included will not work without Strict Checking turned off. Even in VB, bitwise operations only work on integral values. I will wait for some VB experts to chime in, but it looks like your `latitude_decimal_degrees` value is being converted to an int prior to the bit operations. – Perception Dec 18 '12 at 13:53
  • @Perception I checked the "Basic4Android" library and the above seems to be part of that API coded in vb.net. Here is the link for "Basic4Android":http://www.basic4ppc.com/android/wiki/index.php/Bit#ToHexString_.28N_As_Int.29_As_String – Sarah Dec 19 '12 at 04:56
  • I checked it out and posted an answer below. Basic4Android doesn't actually use VB.net so your question changes a bit. The syntax is very close though. – Perception Dec 19 '12 at 05:35

2 Answers2

3

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

Community
  • 1
  • 1
Charlie
  • 7,031
  • 1
  • 37
  • 45
  • Which is more accurate; converting the double to int (I have edited my question with an attempted solution) or converting it to long, please suggest. – Sarah Dec 18 '12 at 07:38
  • Converting to a long would be more "accurate" since double and long are both 64-bits. Converting a double to an int would cause a loss of precision (64 -> 32 bits). For more info on primitive sizes see: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – Charlie Dec 18 '12 at 07:44
  • Thanks for the link. I cannot perform the bitshift on the double or long value. How can I handle that? After the AND operation, I need the bitshift right by 16. – Sarah Dec 18 '12 at 08:28
  • @Sarah the question you need to solve is: what are you going to do *afterwards*? Do you want to decode the data somehow? Otherwise, you might as well send random data... – Has QUIT--Anony-Mousse Dec 18 '12 at 09:36
  • @Anony-Mousse yes ofcourse I will decode the data. See there is a fixed format that we re following here. The app is to be developed in Android as well as BlackBerry. I am doing the BlackBerry app. The format of the message and methods are provided to us that we have to follow strictly. I have vb.net code with me written in 'Basic4Android'. I see the logic in it and write the same in java for BlackBerry. The requirement is to perform the AND operation between the two variables and then apply right bitshift on the result.The server will do the decoding. I have to send the data in this format. – Sarah Dec 18 '12 at 09:41
  • If you want to reproduce the behaviour of VB.net, you better look up in the VB.net manual what they are doing in this case, because that math operation is *not* well defined. It **could** do such a double-to-long conversion. Or it could do `((int)latitude)`, for example. – Has QUIT--Anony-Mousse Dec 18 '12 at 09:58
  • @Sarah - looks like you've accepted an answer, but could you amend your original question to include relevant portions of the vb.net code you are translating? One of the vb experts on here might be able to provide an efficient solution to the problem you are working on. – Perception Dec 18 '12 at 12:21
  • @Perception sure I will do. I accepted the answer because it gave me an idea on how to go about the solution. – Sarah Dec 18 '12 at 13:30
1

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.

Perception
  • 77,470
  • 19
  • 176
  • 187
  • thank you for your time and looking into this question. My doubt since the very beginning has been this that converting a latitude to int would yield incorrect results since we lose the accuracy required in the latitude value. Is this right? A double value of 52.5233 would change to int 52. Is this so? Please confirm. – Sarah Dec 19 '12 at 05:43
  • To be honest, I don't see how this code is working on the Android side right now. There is loss of information everywhere. The cast down to int potentially truncates the doubles value and at a bare minimum you lose the decimal portion. The and operator selects one byte from the high bits of the now truncated int, which for most latitudes will be a zero. That zero is then shifted 16 bits to yield zero yet again. You say this is code from a working program? – Perception Dec 19 '12 at 06:31
  • yes this code is working, both for android and iphone. It was initially developed for the android app. The same has been successfully implemented in the iphone app. I am now applying the same in the BlackBerry app. – Sarah Dec 19 '12 at 07:17