AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

need an unsigned int in java, so...

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
J. David Boyd
Guest





PostPosted: Wed Jul 27, 2005 9:05 pm    Post subject: need an unsigned int in java, so... Reply with quote




what's the workaround?

I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.

The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.

When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.

Any pointers, ideas, or URLs as to what to do to get around this?

Dave in Largo, FL

p.s. I've had no luck with google...


Back to top
Knute Johnson
Guest





PostPosted: Thu Jul 28, 2005 3:32 am    Post subject: Re: need an unsigned int in java, so... Reply with quote



J. David Boyd wrote:
Quote:
what's the workaround?

I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.

The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.

When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.

Any pointers, ideas, or URLs as to what to do to get around this?

Dave in Largo, FL

p.s. I've had no luck with google...



Use a long.

--

Knute Johnson
email s/nospam/knute/

Back to top
J. David Boyd
Guest





PostPosted: Thu Jul 28, 2005 12:53 pm    Post subject: Re: need an unsigned int in java, so... Reply with quote



Knute Johnson <nospam (AT) ljr-2 (DOT) frazmtn.com> writes:

Quote:
J. David Boyd wrote:
what's the workaround?
I've got some legacy C code that I need to convert to java, and some
of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the
password
entered by a user.
When I convert it to java, and run it, sometimes the summation
variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
Dave in Largo, FL
p.s. I've had no luck with google...


Use a long.


I need the number to wrap around, using a long it just keeps on going...


Back to top
Patricia Shanahan
Guest





PostPosted: Thu Jul 28, 2005 1:08 pm    Post subject: Re: need an unsigned int in java, so... Reply with quote

J. David Boyd wrote:
Quote:
Knute Johnson <nospam (AT) ljr-2 (DOT) frazmtn.com> writes:


J. David Boyd wrote:

what's the workaround?
I've got some legacy C code that I need to convert to java, and some
of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the
password
entered by a user.
When I convert it to java, and run it, sometimes the summation
variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
Dave in Largo, FL
p.s. I've had no luck with google...


Use a long.



I need the number to wrap around, using a long it just keeps on going...


At the end, and before any operation that shifts information to the
right, bitwise & with ((1L<<32)-1).

Patricia

Back to top
googmeister@gmail.com
Guest





PostPosted: Thu Jul 28, 2005 2:23 pm    Post subject: Re: need an unsigned int in java, so... Reply with quote

J. David Boyd wrote:
Quote:
what's the workaround?

I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.

The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.

When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.

Any pointers, ideas, or URLs as to what to do to get around this?

I think you should be able to get this to work with regular signed
integers.

What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)


Back to top
J. David Boyd
Guest





PostPosted: Thu Jul 28, 2005 3:06 pm    Post subject: Re: need an unsigned int in java, so... Reply with quote

[email]googmeister (AT) gmail (DOT) com[/email] writes:

Quote:
J. David Boyd wrote:
what's the workaround?


Thanks all, for the tips.

I got the ops I wanted by, (and this might be stupidly redundant, but)...

I used a long (call it LL),

LL <<= 32;
LL >>>= 32;
LL &= 0x00000000FFFFFFFF;


This worked fine.

Dave

Back to top
chris.ebert@gmail.com
Guest





PostPosted: Thu Jul 28, 2005 5:38 pm    Post subject: Re: need an unsigned int in java, so... Reply with quote

A not-elegant workaround: Most C integers are 32 bits. You can do the
math in a Java long (64 bits) and mask it off. Not pretty, but I've
used the trick before.

Back to top
J. David Boyd
Guest





PostPosted: Thu Jul 28, 2005 8:35 pm    Post subject: Re: need an unsigned int in java, so... Reply with quote

[email]chris.ebert (AT) gmail (DOT) com[/email] writes:

Quote:
A not-elegant workaround: Most C integers are 32 bits. You can do the
math in a Java long (64 bits) and mask it off. Not pretty, but I've
used the trick before.

Yes, that is what I ended up doing.

Back to top
Dale King
Guest





PostPosted: Tue Aug 02, 2005 12:22 am    Post subject: Re: need an unsigned int in java, so... Reply with quote

[email]googmeister (AT) gmail (DOT) com[/email] wrote:
Quote:
J. David Boyd wrote:

what's the workaround?

I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.

The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.

When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.

Any pointers, ideas, or URLs as to what to do to get around this?


I think you should be able to get this to work with regular signed
integers.

What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)

multiply and divide are also different.

--
Dale King

Back to top
Patricia Shanahan
Guest





PostPosted: Tue Aug 02, 2005 1:03 am    Post subject: Re: need an unsigned int in java, so... Reply with quote

Dale King wrote:
Quote:
googmeister (AT) gmail (DOT) com wrote:

J. David Boyd wrote:

what's the workaround?

I've got some legacy C code that I need to convert to java, and some
of it
depends on unsigned ints.

The code is doing some kind of stupid crc and xor to encrypt the
password
entered by a user.

When I convert it to java, and run it, sometimes the summation
variables go
negative, changing some of the values in my encrypted string.

Any pointers, ideas, or URLs as to what to do to get around this?



I think you should be able to get this to work with regular signed
integers.

What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)


multiply and divide are also different.


Are you sure about multiply? Java multiplication seems to be a
simple binary multiply, ignoring sign:

"If an integer multiplication overflows, then the result is the
low-order bits of the mathematical product as represented in some
sufficiently large two's-complement format. As a result, if overflow
occurs, then the sign of the result may not be the same as the sign of
the mathematical product of the two operand values."

[http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5036]

The operations I would expect to be an issue are right shifts, division,
and remainder with right hand side not a power of two. In each case,
data flows to lower significance bits.

Patricia

Back to top
Dale King
Guest





PostPosted: Tue Aug 09, 2005 12:05 am    Post subject: Re: need an unsigned int in java, so... Reply with quote

Patricia Shanahan wrote:
Quote:
Dale King wrote:

[email]googmeister (AT) gmail (DOT) com[/email] wrote:
What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)

multiply and divide are also different.

Are you sure about multiply? Java multiplication seems to be a
simple binary multiply, ignoring sign:

Actually, I was second guessing myself when I posted that, but don't
have time to test it myself.

--
Dale King

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.