Unsigned Int in Java

前端 未结 1 1534
执笔经年
执笔经年 2020-12-15 08:36

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,

相关标签:
1条回答
  • 2020-12-15 08:55

    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.
    
    0 讨论(0)
提交回复
热议问题