A slight correction...

There was a bug in the code I proposed. The line

bits = (bits << 8) | getByte();

(or arr[i] instead of 'getByte()', it is the same except for byte order)
should be replaced by:


bits = (bits << 8) | (((long) getByte()) & 0xFF);


Rational: 'byte' and 'long' are signed integer. The 'byte' value is
converted to a 'long' value before the "OR" operation is apply. But when
converting a 'byte' to a 'long', the sign bit is propagated. Consequently,
byte '80' (hexadecimal) is converted to long 'FFFFFFFFFFFFFF80', not
'0000000000000080'. We need to apply a "AND" mask to remove the sign
propagation before to perform the "OR" operation. This behavior can be shown
by the following code:

    byte value = (byte) 0x80;
    System.out.println(Long.toHexString(value));

It will display on console:

    ffffffffffffff80

I encounter this issue I while ago and I forgot it in my last post...


    Martin.



----- Message d'origine -----
De : "ishwari" <[EMAIL PROTECTED]>
Envoyé : mardi 10 juillet 2001 03:48
Objet : Re: Need of Equivalent code in Java


> Thanks a lot Leyland. Ur code is very useful to us and it helped us to
> proceed further.. Thanks again.
>
> Regards,
> ishwari
>
> ----- Original Message -----
> From: "Leyland Needham" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, July 09, 2001 8:51 PM
> Subject: Re: [JAVA3D] Need of Equivalent code in Java
>
>
> > The problem may be because its backwards. That is, it may be an issue of
> big
> > endian and little endian... I'm not sure if this will work... try
> reversing
> > the process and looking at the bits by using the function
> > Double.doubleToLongBits...
> >
> > Also as a quicky you can try reversing the byte order dont know how much
> > that'll help...
> >
> > byte arr[8];
> > long bits=0;
> > double db;
> >
> > for(i=0;i<8;i++)
> >     arr[i] = getbyte();
> >
> > for(i=7;i>=0;i++) // reversed
> >     bits = (bits << 8) | arr[i];
> >
> > db = Double.longBitsToDouble(bits);
> >
> > I dont know if that will work, but its worth a quick try, if it really
is
> an
> > endian issue you may have to do more in terms of reversing the bits of
the
> > individual bytes (yuck). If not, like I said try something like
> >
> > long bits = Double.doubleToLongBits();
> >
> > And then looking at the individual bytes..
> >
> > Leyland Needham

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to