On May 19, 2006, at 2:54 PM, Shane Hathaway wrote:
Hmm...
First, if you like to memorize stuff, you can remember which gets
evaluated first, "<<" or "&". Most of us have better things to
remember. So I'd sprinkle parentheses all over to clarify.
Second, "abyte0[i + 1]" is a byte, so "abyte0[i + 1] << 8" is also
a byte. But you've shifted all its data out, giving you 0. You
need a cast.
Maybe this is what you're after:
public static int get2Little(byte abyte0[], int i)
{
return (((int) abyte0[i + 1]) << 8) | abyte0[i];
}
That might work, unless it trips over an issue with signs. :-)
Shane
I wrote a little test program, and your typecasting issue is not an
issue, and the parentheses are not necessary. I haven't looked at
the spec to see what exactly causes the bytes to get promoted to
ints, but they do. It's probably the fact that a literal number is
interpreted as an int, not a byte, so byteval << intval causes the
byte to be promoted to an int. I ran into this by attempting to pass
literal numbers to a function that took two bytes, and having the
compiler complain that I was passing ints.
--Levi
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/