On Fri, 19 May 2006 at 14:41 -0600, Kimball Larsen wrote:
> Ok, so I've got a degree in CS and all, but it's been since I earned
> that degree that I've ever had to mess with stuff like this. I am
> working on someone else's code (this someone is no longer at my
> disposal) and have the following method:
>
>
> public static int get2Little(byte abyte0[], int i) throws Exception
> {
> return abyte0[i + 1] << 8 & 0xff00 | abyte0[i + 0] & 0xff;
> }
>
> (of course, there are no comments anywhere in the entire class... )
>
> So, a byte[] and index are passed in, the byte at i+1 is anded with
> 0xff00 and shifted 8, then or'ed with the byte at i.
>
> Thus, this appears to be reading 2 bytes out of the array and
> returning them as an int.
>
> So, if the array has 0xC0 as the first byte, and 0x1C as the second
> byte, this should return the integer 49180.
>
> Right?
That's what it looks like to me, except...
> Isn't there a much easier way to do this?
Well that depends on what "this" is. If it's to make an int out of two
bytes in an array in little-endian order, then probably not. Certainly
no more straightforward way. You could be more concise, and if it were
me I would have written the function to take two byte arguments instead.
Something like this C code:
short mkshort(char lsb, char msb)
{
return (msb << 8) | lsb;
}
> Does not seem to be doing what I expect...
I suspect precedence problems. Most likely & has precedence over <<. <<
and >> have fairly low precedence IIRC.
--
Hans Fugal ; http://hans.fugal.net
There's nothing remarkable about it. All one has to do is hit the
right keys at the right time and the instrument plays itself.
-- Johann Sebastian Bach
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/