Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

usigned int java

Thread view: 
Rajesh.Rapaka - 24 Dec 2005 09:09 GMT
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
Roedy Green - 24 Dec 2005 11:44 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.