11package unsigned
22
3- import unsigned.java_1_7.compareUnsigned
4- import unsigned.java_1_7.divideUnsigned
5- import unsigned.java_1_7.parseUnsignedInt
6- import unsigned.java_1_7.remainderUnsigned
73import java.math.BigInteger
84import kotlin.experimental.and
95import kotlin.experimental.inv
@@ -12,15 +8,20 @@ import kotlin.experimental.inv
128 * Created by GBarbieri on 20.03.2017.
139 */
1410
15- class Ubyte (var v : Byte ) : Number() {
11+ class Ubyte (var v : Byte ) : Number(), Comparable<Ubyte> {
1612
1713 companion object {
1814
1915 /* * A constant holding the minimum value an <code>unsigned byte</code> can have, 0. */
20- const val MIN_VALUE = 0x00
16+ const val MIN_VALUE : Int = 0
2117 /* * A constant holding the maximum value an <code>unsigned byte</code> can have, 2<sup>8</sup>-1. */
2218 const val MAX_VALUE : Int = 0xff
2319
20+ /* * A constant holding the minimum value an <code>unsigned byte</code> can have, 0. */
21+ val MIN = Ubyte (0 )
22+ /* * A constant holding the maximum value an <code>unsigned byte</code> can have, 2<sup>8</sup>-1. */
23+ val MAX = Ubyte (0xff )
24+
2425 fun checkSigned (v : Number ) = v.toInt() in MIN_VALUE .. MAX_VALUE
2526 }
2627
@@ -61,11 +62,11 @@ class Ubyte(var v: Byte) : Number() {
6162
6263 infix operator fun div (b : Ubyte ) = Ubyte (toInt() / b.toInt())
6364 infix operator fun div (b : Byte ) = Ubyte (toInt() / b.toUInt())
64- infix operator fun div (b : Int ) = Ubyte (toInt() divideUnsigned b)
65+ infix operator fun div (b : Int ) = Ubyte (toInt() udiv b)
6566
6667 infix operator fun rem (b : Ubyte ) = Ubyte (toInt() % b.toInt())
6768 infix operator fun rem (b : Byte ) = Ubyte (toInt() % b.toUInt())
68- infix operator fun rem (b : Int ) = Ubyte (toInt() remainderUnsigned b)
69+ infix operator fun rem (b : Int ) = Ubyte (toInt() urem b)
6970
7071 // TODO add counterparts with res
7172
@@ -91,11 +92,13 @@ class Ubyte(var v: Byte) : Number() {
9192
9293 fun inv () = Ubyte (v.inv ())
9394
94- operator fun compareTo (b : Ubyte ) = toInt() compareUnsigned b.toInt()
95- operator fun compareTo (b : Byte ) = toInt() compareUnsigned b.toUInt()
96- operator fun compareTo (b : Int ) = toInt() compareUnsigned b
97-
98- override fun toString () = toInt().toString()
95+ override operator fun compareTo (other : Ubyte ) = toInt() compareUnsigned other.toInt()
96+ operator fun compareTo (other : Byte ) = toInt() compareUnsigned other.toUInt()
97+ operator fun compareTo (other : Int ) = toInt() compareUnsigned other
9998
10099 override fun equals (other : Any? ) = other is Ubyte && v == other.v
100+ override fun hashCode (): Int = v.hashCode()
101+ override fun toString () = toInt().toString()
102+ fun toString (radix : Int ) = toInt().toString(radix)
103+ fun toString (format : String ) = format.format(toInt())
101104}
0 commit comments