.\" Made by website/plain/build.mjs from website/words/ushort.md; change that file. .TH SG-USHORT 7 2026-11-28 "SG" "SG reference" .SH NAME ushort \- an integer of 16 bits without a sign, from 0 to 65 535: a port, a field of a packet, a counter that wraps .SH SYNOPSIS .TP \fBushort\fR an integer of 16 bits, from 0 to 65 535, never negative .TP \fBushort port = 8080, 0xffff\fR a number written in the program, where a ushort goes .TP \fBushort.MinValue, ushort.MaxValue\fR the two ends, 0 and 65535 .TP \fBushort w = b, int x = w, uint x = w\fR a byte becomes a ushort on its own, and a ushort any wider type .TP \fB(ushort)n, n.tryToUshort(), n.wrappingToUshort()\fR a wider or signed number made a ushort: a panic, null, or the low 16 bits when it does not fit .TP \fBushort.fromBigEndian(bytes), w.toBigEndian()\fR a ushort out of two bytes, the highest first, and its two bytes; LittleEndian the same, the lowest first .TP \fBw.isPowerOfTwo(), w.log2()\fR the methods of powers of two, which only unsigned types have .TP \fBushort.parse(text), ushort.parseHex(text)\fR the number a text writes, as a Result .SH DESCRIPTION A \fBushort\fR is a whole number from 0 to 65 535 in 16 bits, on every machine. Its range is that of a port of TCP or UDP, and of the fields of two bytes in the headers of a network packet or a file: a length, a count, a checksum. A number written in the program is a \fBushort\fR where a \fBushort\fR goes; one that does not fit, and any negative number, is refused when the program is compiled. .PP Arithmetic happens in the type of its operands, so \fBushort + ushort\fR is a \fBushort\fR, also where the result goes into an \fBint\fR, and \fB1 \- 2\fR on two \fBushort\fRs goes below 0. A result past an end is an overflow: a panic in the interpreter, in tests and in a debug build, and a wrap in a release build. \fBwrappingAdd\fR, \fBtryAdd\fR and \fBsaturatingAdd\fR say one behaviour for every build, and so do the methods of \fB\-\fR and \fB*\fR\&. \fB\-w\fR is refused, since a \fBushort\fR is never negative; \fB>>\fR brings in zeros. .PP A \fBbyte\fR becomes a \fBushort\fR on its own, and a \fBushort\fR becomes an \fBint\fR, a \fBuint\fR, a \fBlong\fR, a \fBulong\fR, an \fBnint\fR, an \fBnuint\fR, a \fBfloat\fR or a \fBdouble\fR\&. A signed type never becomes a \fBushort\fR on its own, not even an \fBsbyte\fR: a \fBshort\fR and a \fBushort\fR do not mix, and an \fBint\fR holds both. The way to a \fBushort\fR is a cast, \fB(ushort)n\fR, which stops the program when the value does not fit (a release build keeps the low 16 bits), or \fBtryToUshort\fR, \fBsaturatingToUshort\fR and \fBwrappingToUshort\fR, which do the same in every build. After \fB\&...\fR in a call of C a \fBushort\fR is refused, since C reads an \fBint\fR there. .PP The methods are in Numbers. .SH EXAMPLES .SS "A port" .RS 4 .nf void show(read string address) { var colon = address.lastIndexOf(':') ?? -1 match ushort.parse(address[colon + 1..]) { Ok(port) => print($"{address}: port {port}") Error(OutOfRange) => print($"{address}: past the last port") Error(Invalid(at)) => print($"{address}: not a port, from byte {at}") } } void main() { ushort web = 8080 print($"{web} {ushort.MinValue} {ushort.MaxValue}") show("localhost:8080") show("[::1]:443") show("example.org:70000") show("example.org:-1") } .fi .RE .PP .RS 4 .nf 8080 0 65535 localhost:8080: port 8080 [::1]:443: port 443 example.org:70000: past the last port example.org:-1: not a port, from byte 0 .fi .RE .PP A port goes from 0 to 65535, as a \fBushort\fR does, so \fBushort.parse\fR tests the range as it reads. A sign is not a digit of an unsigned type: \fB\-1\fR is \fBInvalid\fR, not \fBOutOfRange\fR\&. .SS "The header of a packet" .RS 4 .nf struct UdpHeader { ushort source ushort target ushort length ushort checksum } UdpHeader headerOf(read byte[] b) { return UdpHeader { source = ushort.fromBigEndian(b[0..2]), target = ushort.fromBigEndian(b[2..4]), length = ushort.fromBigEndian(b[4..6]), checksum = ushort.fromBigEndian(b[6..8]), } } void main() { var packet = List { 0xc3, 0x50, 0x00, 0x35, 0x00, 0x1c, 0xab, 0xcd } var h = headerOf(packet[..]) print($"{h.source} -> {h.target}, {h.length} bytes, sum {h.checksum:x4}") ushort port = 53 print(port.toBigEndian()) print(port.toLittleEndian()) } .fi .RE .PP .RS 4 .nf 50000 -> 53, 28 bytes, sum abcd [0, 53] [53, 0] .fi .RE .PP The header of UDP is four \fBushort\fRs, each written the highest byte first, as every number of the network is. \fBfromBigEndian\fR reads exactly two bytes, and \fBtoBigEndian\fR gives them back, where the memory of the machine keeps the lowest first. .SS "The checksum of the internet" .RS 4 .nf ushort checksum(read byte[] data) { uint sum = 0 nint i = 0 while i + 1 < data.length() { sum += ushort.fromBigEndian(data[i..i + 2]) i += 2 } if i < data.length() { sum += (uint)data[i] << 8 } while sum > 0xffff { sum = (sum & 0xffff) + (sum >> 16) } return ~(ushort)sum } void main() { var words = List { 0x00, 0x01, 0xf2, 0x03, 0xf4, 0xf5, 0xf6, 0xf7 } print($"{checksum(words[..]):x4}") var odd = List { 0x01, 0x02, 0x03 } print($"{checksum(odd[..]):x4}") } .fi .RE .PP .RS 4 .nf 220d fbfd .fi .RE .PP IP, TCP and UDP check their headers with this sum of RFC 1071: the data as \fBushort\fRs, added in a wider type, the carries folded back into the low 16 bits, and every bit turned. Each \fBushort\fR widens to a \fBuint\fR as it is added, so the sum cannot overflow; \fB(ushort)sum\fR fits once the carries are folded. The first sum is the example of the RFC. .SS "A counter that wraps" .RS 4 .nf bool newer(ushort a, ushort b) { short ahead = b.wrappingSubtract(a).wrappingToShort() return ahead > 0 } void main() { ushort seq = 65534 for _ in 0..4 { print(seq) seq = seq.wrappingAdd(1) } print($"{newer(65535, 1)} {newer(1, 65535)} {newer(100, 200)}") } .fi .RE .PP .RS 4 .nf 65534 65535 0 1 true false true .fi .RE .PP A packet of sound or video carries a number of 16 bits that goes up by one and starts again at 0, and the receiver tells which of two packets is newer. \fBwrappingAdd\fR goes from 65535 to 0 in every build. The distance from \fBa\fR to \fBb\fR, wrapped and read with a sign, is positive when \fBb\fR is ahead by less than half the range: 1 comes after 65535. .SS "Wider types and bits" .RS 4 .nf void main() { byte low = 0x34 byte high = 0x12 ushort word = high word = word << 8 | low print($"{word:x4}") int asInt = word uint asUint = word float asFloat = word print($"{asInt + 1} {asUint * 2} {asFloat / 2}") print($"{word.countOnes()} {word.isPowerOfTwo()} {word.nextPowerOfTwo()} {word.log2()}") } .fi .RE .PP .RS 4 .nf 1234 4661 9320 2330 5 false 8192 12 .fi .RE .PP A \fBbyte\fR becomes a \fBushort\fR on its own, and the shift happens in 16 bits, the type of its left side; \fBlow\fR widens for the \fB|\fR\&. A \fBushort\fR becomes an \fBint\fR, a \fBuint\fR or a \fBfloat\fR on its own. The powers of two are methods of the unsigned types only. .SH "WHAT STOPS THE PROGRAM" .SS "A sum that goes into an int" .RS 4 .nf void main() { ushort a = 60_000 ushort b = 10_000 int total = a + b print(total) } .fi .RE .PP .RS 4 .nf panic: overflow: 60000 + 10000 does not fit in ushort --> example.sg:4:17 4 | int total = a + b | ^^^^^ hint: the result goes into an `int`; compute in `int` from the start: `(int)a + b` .fi .RE .PP The sum is a \fBushort\fR, since both sides are, and becomes an \fBint\fR only after it is found; a release build wraps it to 4464. \fB(int)a + b\fR adds in an \fBint\fR and gives 70000. .SS "A negative number, cast" .RS 4 .nf ushort toPort(int n) { return (ushort)n } void main() { print(toPort(443)) print(toPort(-1)) } .fi .RE .PP .RS 4 .nf 443 panic: value -1 does not fit in ushort --> example.sg:2:12 2 | return (ushort)n | ^^^^^^^^^ hint: `tryToUshort()` gives `null` when the value does not fit, and `wrappingToUshort()` cuts it to size .fi .RE .PP A release build keeps the low 16 bits, so -1 becomes 65535. \fBn.tryToUshort()\fR gives \fBnull\fR for it in every build. .SH "WHAT THE COMPILER REFUSES" .SS "A negative number" .RS 4 .nf void main() { ushort port = -1 print(port) } .fi .RE .PP .RS 4 .nf error: `-1` does not fit in ushort a `ushort` is never negative --> example.sg:2:19 2 | ushort port = -1 | ^^ .fi .RE .PP \fB(ushort)\-1\fR is refused the same way. \fBushort.MaxValue\fR is the largest. .SS "A number too large" .RS 4 .nf void main() { ushort mask = 0x1_0000 print(mask) } .fi .RE .PP .RS 4 .nf error: `0x1_0000` is 65536, which does not fit in ushort --> example.sg:2:19 2 | ushort mask = 0x1_0000 | ^^^^^^^^ .fi .RE .SS "A minus" .RS 4 .nf void main() { ushort port = 8080 print(-port) } .fi .RE .PP .RS 4 .nf error: operator `-` is not defined for `ushort`: a `ushort` is never negative --> example.sg:3:11 3 | print(-port) | ^^^^^ hint: convert first: `-(int)port` .fi .RE .SS "A signed type where a ushort goes" .RS 4 .nf void main() { short offset = 10 ushort port = offset print(port) } .fi .RE .PP .RS 4 .nf error: expected `ushort`, found `short` --> example.sg:3:19 3 | ushort port = offset | ^^^^^^ hint: convert with `(ushort)offset`; a value that does not fit panics, or is cut to size in a release build .fi .RE .PP A \fBshort\fR can be negative, so it never becomes a \fBushort\fR on its own, even where its value is 10. .SS "An int where a ushort goes" .RS 4 .nf ushort portOf(int n) { return n } void main() { print(portOf(8080)) } .fi .RE .PP .RS 4 .nf error: expected `ushort`, found `int` --> example.sg:2:12 2 | return n | ^ hint: convert with `(ushort)n`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SH "SEE ALSO" .BR sg-short (7), .BR sg-byte (7), .BR sg-uint (7), .BR sg-int (7), .BR sg-packed (7) .PP https://sg\-lang.org/reference/ushort.html