.\" Made by website/plain/build.mjs from website/words/short.md; change that file. .TH SG-SHORT 7 2026-11-28 "SG" "SG reference" .SH NAME short \- an integer of 16 bits, with a sign, from -32 768 to 32 767: a sample of sound, a field of two bytes .SH SYNOPSIS .TP \fBshort\fR an integer of 16 bits, from -32 768 to 32 767 .TP \fBshort s = \-12_000, 0x7fff\fR a number written in the program, where a short goes .TP \fBshort.MinValue, short.MaxValue\fR the two ends .TP \fBint x = s, double x = s\fR a short becomes a wider signed type, or a fraction, on its own .TP \fB(short)n, n.tryToShort(), n.saturatingToShort()\fR a wider number made a short: a panic, null, or the nearest end when it does not fit .TP \fBa.saturatingAdd(b), a.wrappingAdd(b)\fR arithmetic that stops at an end, or wraps, in every build .TP \fBshort.fromLittleEndian(bytes), s.toLittleEndian()\fR a short out of two bytes, and its two bytes; BigEndian the same, the highest first .TP \fBshort.parse(text)\fR the number a text writes, as a Result .SH DESCRIPTION A \fBshort\fR is a whole number from -32 768 to 32 767 in 16 bits, two's complement, on every machine. It is the type of a sample of sound in 16 bits, as a WAV file and a sound card keep it, of a signed field of two bytes in a file or a packet, and of C's \fBshort\fR\&. A list of them takes half the memory of \fBint\fRs. A number written in the program is a \fBshort\fR where a \fBshort\fR goes, and one that does not fit is refused when the program is compiled. .PP Arithmetic happens in the type of its operands, so \fBshort + short\fR is a \fBshort\fR, also where the result goes into an \fBint\fR\&. 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. For sound a wrap is the worst answer, since a loud peak turns into a loud peak of the other sign; \fBsaturatingAdd\fR stops at the end instead, in every build, and \fBtryAdd\fR and \fBwrappingAdd\fR say the other two behaviours. \fBshort.MinValue / \-1\fR stops the program in every build. .PP A \fBshort\fR becomes an \fBint\fR, a \fBlong\fR, an \fBnint\fR, a \fBfloat\fR or a \fBdouble\fR on its own, and a \fBbyte\fR or an \fBsbyte\fR becomes a \fBshort\fR\&. It never becomes an unsigned type on its own: a \fBshort\fR and a \fBushort\fR do not mix, and an \fBint\fR holds both. The way back from a wider type is a cast, \fB(short)n\fR, which stops the program when the value does not fit (a release build keeps the low 16 bits of an integer, and takes the nearest end for a fraction), or \fBtryToShort\fR, \fBsaturatingToShort\fR and \fBwrappingToShort\fR, which do the same in every build. After \fB\&...\fR in a call of C a \fBshort\fR is refused, since C reads an \fBint\fR there. .PP The methods are in Numbers. .SH EXAMPLES .SS "A number of 16 bits" .RS 4 .nf void main() { short sample = -12_000 short quiet = 0x00ff print($"{sample} {quiet} {short.MinValue} {short.MaxValue}") print($"{Memory.sizeOf()} {Memory.sizeOf()}") int wide = sample double level = sample print($"{wide * 10} {level / short.MaxValue}") print($"{sample:x4} {sample:X}") } .fi .RE .PP .RS 4 .nf -12000 255 -32768 32767 2 4 -120000 -0.36622211371196634 d120 D120 .fi .RE .PP A \fBshort\fR takes two bytes and an \fBint\fR four. \fBsample\fR widens to an \fBint\fR and to a \fBdouble\fR on its own, so \fBwide * 10\fR is an \fBint\fR and \fBlevel / short.MaxValue\fR a fraction. In hex a negative \fBshort\fR is written as its 16 bits. .SS "Samples out of bytes" .RS 4 .nf void main() { var data = List { 0x00, 0x00, 0xe8, 0x03, 0x18, 0xfc, 0x00, 0x80, 0xff, 0x7f } var samples = List() for i in 0..data.length() / 2 { samples.add(short.fromLittleEndian(data[2 * i..2 * i + 2])) } print(samples) ushort peak = 0 for s in read samples { peak = peak.max(s.unsignedAbs()) } print(peak) var out = List() for s in read samples { out.addAll(read s.toLittleEndian()) } print(out == data) } .fi .RE .PP .RS 4 .nf [0, 1000, -1000, -32768, 32767] 32768 true .fi .RE .PP The samples of a WAV file lie in pairs of bytes, the lowest first. \fBfromLittleEndian\fR takes exactly two and reads the highest bit as the sign. The loudest sample here is -32768, whose size does not fit in a \fBshort\fR: \fBabs\fR would overflow, and \fBunsignedAbs\fR gives it as a \fBushort\fR\&. \fBtoLittleEndian\fR gives the two bytes back. .SS "Mixing two sounds" .RS 4 .nf short mix(short a, short b) { return a.saturatingAdd(b) } void main() { var voice = List { 1000, 20_000, -25_000, 30_000 } var music = List { 500, 15_000, -15_000, 10_000 } for i in 0..voice.length() { var a = voice[i] var b = music[i] print($"{mix(a, b)} {a.wrappingAdd(b)}") } } .fi .RE .PP .RS 4 .nf 1500 1500 32767 -30536 -32768 25536 32767 -25536 .fi .RE .PP Two sounds are mixed by adding their samples. Where the sum passes an end, \fBsaturatingAdd\fR holds it there, which is heard as clipping; \fBwrappingAdd\fR jumps to the other sign, which is heard as a crack. Both do the same in every build. .SS "Louder, and the average" .RS 4 .nf void main() { var samples = List { 12_000, -32_768, 32_767, -400 } double gain = 1.5 var louder = List() for s in read samples { louder.add((s * gain).saturatingToShort()) } print(louder) long sum = 0 for s in read samples { sum += s } print($"{sum} {(double)sum / (double)samples.length()}") } .fi .RE .PP .RS 4 .nf [18000, -32768, 32767, -600] 11599 2899.75 .fi .RE .PP In \fBs * gain\fR the \fBshort\fR becomes a \fBdouble\fR on its own, since a \fBdouble\fR holds every \fBshort\fR exactly. \fBsaturatingToShort\fR brings the product back, the nearest end where it does not fit. A sum of many samples is kept in a \fBlong\fR, and each \fBshort\fR widens as it is added. .SS "A release build wraps" The file clip/main.sg: .PP .RS 4 .nf short mix(short a, short b) { return a + b } void main() { print(mix(20_000, 15_000)) } .fi .RE .PP .RS 4 .nf $ cd clip $ sg main.sg panic: overflow: 20000 + 15000 does not fit in short --> main.sg:2:12 2 | return a + b | ^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around $ sg build --release main.sg -o mix $ ./mix -30536 .fi .RE .PP The interpreter and a debug build stop at the overflow. A release build leaves the check out and the sum wraps to the other sign. Where an overflow can happen, as when sounds are mixed, the program says what it wants with \fBsaturatingAdd\fR\&. .SH "WHAT STOPS THE PROGRAM" .SS "A fraction too large, cast" .RS 4 .nf short toSample(double x) { return (short)(x * 32767) } void main() { print(toSample(0.5)) print(toSample(1.5)) } .fi .RE .PP .RS 4 .nf 16383 panic: value 49150.5 does not fit in short --> example.sg:2:12 2 | return (short)(x * 32767) | ^^^^^^^^^^^^^^^^^^ hint: `tryToShort()` gives `null` when the value does not fit .fi .RE .PP Sound as fractions goes from -1 to 1, and 1.5 is past the end. A release build takes the nearest end, 32767; \fB(x * 32767).saturatingToShort()\fR does that in every build. .SS "A sum that goes into an int" .RS 4 .nf void main() { short a = 30_000 short b = 10_000 int total = a + b print(total) } .fi .RE .PP .RS 4 .nf panic: overflow: 30000 + 10000 does not fit in short --> 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 \fBshort\fR, since both sides are, and becomes an \fBint\fR only after it is found. \fB(int)a + b\fR adds in an \fBint\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "A number that does not fit" .RS 4 .nf void main() { short sample = 40_000 print(sample) } .fi .RE .PP .RS 4 .nf error: `40_000` does not fit in short --> example.sg:2:20 2 | short sample = 40_000 | ^^^^^^ .fi .RE .PP \fBshort s = 0xffff\fR is refused the same way: it is 65535. .SS "An int added to a short" .RS 4 .nf void main() { short sample = 1000 int step = 24 sample += step print(sample) } .fi .RE .PP .RS 4 .nf error: expected `short`, found `int` --> example.sg:4:15 4 | sample += step | ^^^^ hint: convert with `(short)step`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A short and a ushort" .RS 4 .nf void main() { short sample = -1200 ushort offset = 300 print(sample + offset) } .fi .RE .PP .RS 4 .nf error: `short` and `ushort` do not mix: neither holds every value of the other --> example.sg:4:11 4 | print(sample + offset) | ^^^^^^^^^^^^^^^ hint: `int` holds both: `(int)offset` .fi .RE .SS "A window of the wrong width" .RS 4 .nf void main() { var data = List { 0xe8, 0x03, 0x18, 0xfc } print(short.fromLittleEndian(data[0..4])) } .fi .RE .PP .RS 4 .nf error: `short.fromLittleEndian` reads 2 bytes, and this window has 4 --> example.sg:3:34 3 | print(short.fromLittleEndian(data[0..4])) | ^^^^^^^^^^ hint: a window of 2 bytes: `data[0..2]` .fi .RE .PP A window whose width is known only when the program runs stops it there, with both numbers. .SS "A short after ... in a call of C" .RS 4 .nf extern { int printf(read CString format, ...) } void main() { short level = -12 var format = CString.from("%d\en") ?? panic("a zero byte inside") _ = unsafe printf(read format, level) } .fi .RE .PP .RS 4 .nf error: a `short` does not go to C after `...` C reads it there as an `int`, made wider without a word --> example.sg:8:36 8 | _ = unsafe printf(read format, level) | ^^^^^ hint: `(int)level` .fi .RE .PP An \fBsbyte\fR, a \fBbyte\fR and a \fBushort\fR are refused there the same way. .SS "A shift past 15" .RS 4 .nf void main() { short bits = 1 print(bits << 16) } .fi .RE .PP .RS 4 .nf error: a shift by 16 is out of range: `short` has 16 bits --> example.sg:3:19 3 | print(bits << 16) | ^^ hint: the count goes from 0 to 15 .fi .RE .PP \fBint bits = 1\fR shifts in 32 bits. .SH "SEE ALSO" .BR sg-ushort (7), .BR sg-sbyte (7), .BR sg-int (7), .BR sg-double (7), .BR sg-packed (7) .PP https://sg\-lang.org/reference/short.html