On Feb 18, 12:54 am, Rizvan S <[email protected]> wrote:
> On Wed, Feb 18, 2009 at 12:41 AM, Jiang <[email protected]> wrote:
> > long a = 0x1234567890abL;
> > long b = (a & 0xFFFFFFFF);
> > I expect the value of b is 0x567890abL, but in practice, it is still
> > 0x1234567890abL.
>
> Output :
>
> ubuntu:~$ ./t32bit
> b =567890ab
>
> I am getting correct output.
The correct output for the original implementation (here in compilable
form):
public class Foo {
public static void main(String[] args) {
long a = 0x1234567890abL;
long b = (a & 0xFFFFFFFF);
System.out.println("b is " + Long.toHexString(b));
}
}
is:
b is 1234567890ab
As the original poster noted in his update, a missing 'L' on the
constant is the source of the trouble. Because all primitive types
except "char" are signed, 0xffffffff is sign-extended to
0xffffffffffffffffL, and 'b' remains unchanged. Appending 'L' to the
constant yields the desired behavior.
This happens a lot in Java, usually when you're pulling binary data
out of a file.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---