On 24 Dec 2005 01:09:51 -0800, "Rajesh.Rapaka"
<rajesh.rapaka@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>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

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Googmeister - 24 Dec 2005 14:55 GMT
> On 24 Dec 2005 01:09:51 -0800, "Rajesh.Rapaka"
> <rajesh.rapaka@gmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 4 lines]
> >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.
I think you mean division/remainder instead of multiplication.
I'd also add casting to the list.
Roedy Green - 24 Dec 2005 15:12 GMT
>> 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.
compare, multiplication, division, remainder all are different from a
bit point of view for signed/unsigned. addition, subtraction, move are
not.
see http://mindprod.com/jgloss/unsigned.html for details.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Googmeister - 26 Dec 2005 15:13 GMT
> >> when transferring bytes an int will do for unsigned. It is only when
> >> you do a multiplication or compare does signed/unsigned matter.
[quoted text clipped - 5 lines]
> bit point of view for signed/unsigned. addition, subtraction, move are
> not.
Are you sure about multiplication? Can you provide
a counterexample? Thanks.
Roedy Green - 26 Dec 2005 17:17 GMT
>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.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Mark Thornton - 28 Dec 2005 10:12 GMT
>>Are you sure about multiplication? Can you provide
>>a counterexample? Thanks.
[quoted text clipped - 9 lines]
> 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
Googmeister - 28 Dec 2005 12:11 GMT
> >>Are you sure about multiplication? Can you provide
> >>a counterexample? Thanks.
[quoted text clipped - 12 lines]
> 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).
Roedy Green - 28 Dec 2005 15:36 GMT
On Wed, 28 Dec 2005 10:12:57 GMT, Mark Thornton
<mark.p.thornton@ntl-spam-world.com> wrote, quoted or indirectly
quoted someone who 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.
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 );

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.