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.

unsigned java type

Thread view: 
palmis - 01 Dec 2005 16:14 GMT
Hi,
I want to use unsigned java type in java but java doesn't support it.
How can I do?
VisionSet - 01 Dec 2005 16:21 GMT
> Hi,
> I want to use unsigned java type in java but java doesn't support it.
> How can I do?

Use char which is unsigned 16 bit or restate your real problem.

--
Mike W
palmis - 02 Dec 2005 11:46 GMT
I try to explain my problem:
I capture, with of the get, the data from a structure MIB in which they
they are stored.
The method that I use, gives back a Object to me.
I must packet this values in a message with a structure the whose
fields have different types (unsigned int bit, unsigned int 16 bit,
unsigned int 32 bit......) second the detailed lists of the used
protocol.
Although java it has various types primiti to you, they are with the
sign. On Internet I have found a way in order to obtain in java types
without sign. Using the masks of the type:

int i;                            //int 32-bit, long 64-bit
long l=i&0xfffffffl;           //to get to 32-bit unsigned

Now I'm asked to me:  java has to max types double (64 bit) with which
I could obtain float (32 bit) without sign, how can I  obtain in java
64 bit types without sign? Perhaps there is in any case a limit and if
the only way were this to proceed, I do not have much familiarity with
these masks.
Hope I have been clear.
Thomas Hawtin - 02 Dec 2005 15:10 GMT
> Now I'm asked to me:  java has to max types double (64 bit) with which
> I could obtain float (32 bit) without sign, how can I  obtain in java
> 64 bit types without sign? Perhaps there is in any case a limit and if
> the only way were this to proceed, I do not have much familiarity with
> these masks.

Java primitives only support integers up to Long.MAX_VALUE. If you want
more than this, there are a few options:

Use a class that supports larger values. For instance,
java.math.BigInteger will support values up to around 2^(2^31). If you
just wanted values from 0 to 2^64-1 you could write a more efficient class.

Alternatively, you could go for the unencapsulated version of the above.
Write static methods that operate on the underlying data.

Or, write your own minilanguage for manipulating these values. Or borrow
someone else's.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 02 Dec 2005 19:10 GMT
>Now I'm asked to me:  java has to max types double (64 bit) with which
>I could obtain float (32 bit) without sign, how can I  obtain in java
>64 bit types without sign?

see http://mindprod.com/unsigned.html

Addition and subtraction work the same way for both signed and
unsigned, so you automatically already have signed and unsigned.
there is no 64x64 primitive multiply so it does not matter whether you
had signed or unsigned 64 bit.

That leaves division.  Which you could handle by mimicking the way you
did division in the 4th grade, only 32 bits at a time instead of a
decimal digit at a time.

Signature

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

palmis - 05 Dec 2005 08:23 GMT
But what happens if I put the unsigned data so obtain, into a byte
array (byte[])? Will it be put with sign?
Thomas Weidenfeller - 05 Dec 2005 08:59 GMT
> But what happens if I put the unsigned data so obtain, into a byte
> array (byte[])? Will it be put with sign?

"sign" is in the eye of the beholder ...

What all primitive types (byte, short, int, long) first of all do is to
store a bunch of bits (8, 16, 32, 64 bits in Java). One of this bits in
a data type, the sign bit, is interpreted special during several
operations. For example, if you ask Java to print out the value, or do
some multiplication, Java - like many other languages - looks at the
sign bit to decide if the number should be treated as negative.

All you have to do when trying to work with unsigned numbers is to
either avoid running into these special interpretations, or to
compensate for them. Typically, for example, you mask with 0xFF to avoid
 the sign extension when a byte is implicitly or explicitly converted
to int. And voila, your bytes appear as unsigned in the integer number.

Getting this right just requires to understand how integer numbers in
computers work in general. And in detail, what the two's-complement of a
number is, its relation to the one's-complement, and what sign-extension
is and when it happens. Nothing which one can't look up in 15 minutes.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Chris Uppal - 05 Dec 2005 11:22 GMT
> Getting this right just requires to understand how integer numbers in
> computers work in general.

/just/ requires ?  Seems a little under-stated to me.  Manipulating unsigned
quantities held in signed[*] variables is confusing, hard to debug, and hard to
maintain.  Or at least, that's /my/ experience.

   -- chris

([*] the first version of this post had that word misspelled as "singed" -- and
indeed it is easy to get burned ;-)
Roedy Green - 05 Dec 2005 19:11 GMT
>But what happens if I put the unsigned data so obtain, into a byte
>array (byte[])? Will it be put with sign?

All that are in a byte are bits.  The signedness is how the high bit
is sometimes treated in a byte.

One way it is treated is to be propagated when promoted to int.  So
you have to be vigilant and do an & 0xff to chop it back off.

in adding, signed and unsigned are treated identically, so it does not
matter.

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

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

Chris Smith - 01 Dec 2005 17:22 GMT
> I want to use unsigned java type in java but java doesn't support it.
> How can I do?

I find it useful to look at things in a different way.  Java's integer
variables are of ambiguous sign, but it's EXPRESSIONS and APIs that
treat their operands as being of one or another sign.  Some common
operations on integer types include:

   +, -, ++, --, <<, ==, !=:       either signed or unsigned
   narrowing primitive conversion: either signed or unsigned
   *, /, %, >>, <, >, <=, >=:      signed, except for char
   widening primitive conversion:  signed, except for char
   String conversion:              signed, except for char
   >>>:                            always unsigned

and some APIs:

   DataOutputStream                either signed or unsigned
   PrintStream/PrintWriter         signed, except for char

Generally, it's possible to do quite a lot with "signed" Java data types
even if you're working with unsigned values of the same bit count.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 01 Dec 2005 18:27 GMT
>I want to use unsigned java type in java but java doesn't support it.
>How can I do?

see http://mindprod.com/jgloss/unsigned.html
Signature

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

palmis - 02 Dec 2005 11:49 GMT
Thank you,
I had found this link before.


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



©2008 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.