From byte to long
Hi
A very easy question. Imagine the byte 0xFF (Or 11111111). I want to build a long (64 bits length) having this byte on the last 8 bits starting from the right. That is:
0000000....0000011111111
If a write long l = (long) b, then l is -1 (Or 111111...11111). Is there any way to do this? Thanks in advance.
[334 byte] By [
astu18a] at [2007-11-27 11:55:35]

> Hi
>
> A very easy question. Imagine the byte 0xFF (Or
> 11111111). I want to build a long (64 bits length)
> having this byte on the last 8 bits starting from the
> right. That is:
>
> 0000000....0000011111111
>
> If a write long l = (long) b, then l is -1 (Or
> 111111...11111). Is there any way to do this? Thanks
> in advance.
Keep the cast, and use bitwise and to mask off the most significant bits.
Kaj
kajbja at 2007-7-29 19:02:48 >

Your Question is not clear...can u make it a little bit clear so that i can help u..
How can OXFF is a byte...its decimal value is 255 and 0XFF is hexaDecimal representation if ypu use int instead of byte you will get correct answer...try it out in that way and let me know further queries
> byte b = (byte)0xff;
> long l = b;
> l &= 0xff;
This solution works but this is not what I am looking for. Imagine that you do not know the value of the byte. This byte is only a variable. And the purpose is to set this byte at the end of the long bit String, that is, the first 8 bits starting from the right. Any ideas?
> > byte b = (byte)0xff;
> > long l = b;
> > l &= 0xff;
>
> This solution works but this is not what I am looking
> for. Imagine that you do not know the value of the
> byte. This byte is only a variable. And the purpose
> is to set this byte at the end of the long bit
> String, that is, the first 8 bits starting from the
> right. Any ideas?
That's exactly what the code does. You should only change the value of b.
Kaj
kajbja at 2007-7-29 19:02:48 >
