usigned int java

R

Rajesh.Rapaka

Hi,

I need to used an unsigned integer in my program. I am actually
developing a protocol which at a certain points asks me to transfer 4
bytes unsigned. long makes 8 bytes. How can I transfer the unsigned int
value.

any help or info is appreciated.
regards
Rajesh Rapaka
 
R

Roedy Green

I need to used an unsigned integer in my program. I am actually
developing a protocol which at a certain points asks me to transfer 4
bytes unsigned. long makes 8 bytes. How can I transfer the unsigned int
value.

when transferring bytes an int will do for unsigned. It is only when
you do a multiplication or compare does signed/unsigned matter.

See http://mindprod.com/jgloss/unsigned.html
 
G

Googmeister

when transferring bytes an int will do for unsigned. It is only when
you do a multiplication or compare does signed/unsigned matter.

I think you mean division/remainder instead of multiplication.
I'd also add casting to the list.
 
G

Googmeister

Roedy said:
compare, multiplication, division, remainder all are different from a
bit point of view for signed/unsigned. addition, subtraction, move are
not.

Are you sure about multiplication? Can you provide
a counterexample? Thanks.
 
R

Roedy Green

Are you sure about multiplication? Can you provide
a counterexample? Thanks.

In Forth there are signed * and unsigned U* multiplication operators.
I implemented them in BBL Forth. There I was multiplying to 32 bit
quantities to get a 64 bit result.

If you have a hex calculator with more than 32 bits, try some
experiments with two +, two - and mixed, treated both as unsigned and
signed. pick some large and small numbers.

Or try the same in Java with 16 bits where you have char and short to
play with to give you a 32 bit result.
 
M

Mark Thornton

Roedy said:
In Forth there are signed * and unsigned U* multiplication operators.
I implemented them in BBL Forth. There I was multiplying to 32 bit
quantities to get a 64 bit result.

If you have a hex calculator with more than 32 bits, try some
experiments with two +, two - and mixed, treated both as unsigned and
signed. pick some large and small numbers.

Or try the same in Java with 16 bits where you have char and short to
play with to give you a 32 bit result.

The low order 32 bits of a 32 bit multiply is the same for signed and
unsigned. All that differs is testing for overflow.

Mark Thornton
 
G

Googmeister

Mark said:
The low order 32 bits of a 32 bit multiply is the same for signed and
unsigned. All that differs is testing for overflow.

Thanks, that's the same conclusion I reached. Since Java truncates
overflow, you get exactly the same bits (just like for add/subtract).
 
R

Roedy Green

The low order 32 bits of a 32 bit multiply is the same for signed and
unsigned. All that differs is testing for overflow.

Here is an example demonstrating this is so. A 32x32->64 bit differs
between signed and unsigned however.

// unsigned 32-bit multiply
long a = ( 0x7fffffffL * 0xffffffffL ) & 0xffffffffL;

// signed 32-bit multiply
long b = ((long) ( 0x7fffffff * 0xffffffff )) & 0xffffffffL;

// prints 2147483649
System.out.println( a );
// prints 2147483649
System.out.println( b );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top