Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

networking - Unsigned Int in Java

I'm trying to implement an existing network protocol which makes heavy use of Unsigned datatypes, which are not supported by Java. What I currently do is for each datatype, chose the next bigger one so that the unsigned number may fit into the positive region and then use byte shifting to get the desired effect. Since this is pretty error prone and for an unsigned long onward I have to use BigInteger which is a lot heavier than expanded types, I was wondering if there isn't a better way to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Depending on what you are doing, you can just treat long as a 64-bit value and int as a 32-bit value. Most operations esp readInt/Long writeInt/Long work just the same by ignoring the signness.

Can you give an example of operation you perform on these numbers and perhaps we can suggest how would do the same thing without having to expand the type.

For example, ++, --, +, -, *, ==, !=, << all work the same regardless of signess (i.e. give the same answer). for >> you can substitue >>>

It is the /, %, >, >=, <, <= and printing functions which assume signed values, but you should be able to work around these (if you use these).

e.g.

long unsignedA = 
long unsignedB = 
boolean greater = unsignedA + Long.MIN_VALUE > unsignedB + Long.MIN_VALUE

EDIT: Why does this work? Partly because java doesn't have overflow/underflow exceptions.

e.g.

byte unsignedA = 0;
unsignedA--; 
// unsignedA == FF, is this -1 or 255? Java assumes the former but you assume the later

byte unsignedB = unsignedA * unsignedA;
// unsignedB is -1 * -1 = 1 or (byte) (255*255) = (byte) 65525 = 1.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...