tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Denis Haskin <>
Subject Re: Signed vs. unsigned ints in Java
Date Wed, 19 Jun 2002 16:02:43 GMT
(Perhaps we should take this offline since it's not Tomcat-specific.)

Although Java will always give you back the signed representation of a
byte/int/whatever, you can actually set the unsigned values you want by using
casting to avoid the "loss of precision" error.

E.g. if I say:
    byte b = (byte)0xFF;
    byte b = (byte)256;
then all 8 bits in the byte get set (and of course if I print it out Java says
the value is -1.

Then it's simple matter of bit masking and shifting to select the bits you want
and put them in the right place in your protocol header.

I assume you don't really want a *String* of bits, right?  That threw Yoav off


> Joe Tomcat wrote:
> I need to write some code that forms headers for a protocol.  This protocol
> requires specifying certain values as 16 bit unsigned ints, or even 14 bit
> unsigned values.  All the int types in java are signed.  Is there a way that
> I can take a regular 32 bit java int and come up with the string of 16 or 14
> or 4 (depending on which header field) bits that represent that value in an
> unsigned way?

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message