.\" Made by website/plain/build.mjs from website/words/sbyte.md; change that file. .TH SG-SBYTE 7 2026-11-28 "SG" "SG reference" .SH NAME sbyte \- an integer of 8 bits, with a sign, from -128 to 127: a small step, a reading, a signed byte of a file .SH SYNOPSIS .TP \fBsbyte\fR an integer of 8 bits, from -128 to 127 .TP \fBsbyte t = \-40, 0x7f, 0b0101\fR a number written in the program, where an sbyte goes .TP \fBsbyte.MinValue, sbyte.MaxValue\fR the two ends .TP \fBshort x = t, int x = t, double x = t\fR an sbyte becomes a wider signed type, or a fraction, on its own .TP \fB(sbyte)n, n.tryToSbyte(), n.saturatingToSbyte()\fR a wider number made an sbyte: a panic, null, or the nearest end when it does not fit .TP \fBb.wrappingToSbyte(), t.wrappingToByte()\fR the bits of a byte read with a sign, and back .TP \fBraw sbyte\fR a pointer to C's signed char .TP \fBsbyte.parse(text)\fR the number a text writes, as a Result .SH DESCRIPTION An \fBsbyte\fR is a whole number from -128 to 127 in 8 bits, two's complement, on every machine. It is for a small value with a sign where the space counts: the step between two readings, a temperature, an offset, a signed byte of a file format or of a device. A number written in the program is an \fBsbyte\fR where an \fBsbyte\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 \fBsbyte + sbyte\fR is an \fBsbyte\fR, and 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. A number written beside an \fBsbyte\fR takes its type too, so \fBt + 200\fR and \fBt < 200\fR are refused: 200 is not an \fBsbyte\fR\&. \fBwrappingAdd\fR, \fBtryAdd\fR and \fBsaturatingAdd\fR say one behaviour for every build, and so do the methods for \fB\-\fR, \fB*\fR and \fBabs\fR\&. \fBsbyte.MinValue / \-1\fR stops the program in every build. .PP An \fBsbyte\fR becomes a \fBshort\fR, an \fBint\fR, a \fBlong\fR, an \fBnint\fR, a \fBfloat\fR or a \fBdouble\fR on its own, since each holds every one of its values. It never becomes an unsigned type on its own, not even a \fBbyte\fR: an \fBsbyte\fR and a \fBbyte\fR do not mix, and a \fBshort\fR holds both. The way back from a wider type is a cast, \fB(sbyte)n\fR, which stops the program when the value does not fit (a release build keeps the low 8 bits), or a method that does the same in every build. .PP A \fBbyte\fR and an \fBsbyte\fR have the same bits for 0 to 127. \fBb.wrappingToSbyte()\fR reads all 8 bits of a \fBbyte\fR as an \fBsbyte\fR, so 0xfb is -5, and \fBt.wrappingToByte()\fR goes back; \fB(sbyte)b\fR stops the program for a \fBbyte\fR above 127. One byte has no order, so an \fBsbyte\fR has no \fBtoLittleEndian\fR or \fBfromBigEndian\fR\&. After \fB\&...\fR in a call of C, an \fBsbyte\fR is refused with the hint \fB(int)t\fR, since C reads an \fBint\fR there. .PP The methods are in Numbers. .SH EXAMPLES .SS "A number from -128 to 127" .RS 4 .nf void main() { sbyte low = -40 sbyte high = 45 print($"{low} {high} {high - low}") print($"{sbyte.MinValue} {sbyte.MaxValue}") int wide = low double fraction = low print($"{wide * 1000} {fraction / 3}") print($"{low:x2} {low:b8} {high:x2}") } .fi .RE .PP .RS 4 .nf -40 45 85 -128 127 -40000 -13.333333333333334 d8 11011000 2d .fi .RE .PP \fBhigh \- low\fR is an \fBsbyte\fR, and 85 fits. \fBwide * 1000\fR is an \fBint\fR, since \fBlow\fR widened on its way into \fBwide\fR\&. In hex and in binary a negative \fBsbyte\fR is written as its 8 bits. .SS "Small steps between readings" .RS 4 .nf List? steps(read List readings) { var out = List() for i in 1..readings.length() { int step = (int)readings[i] - readings[i - 1] var small = step.tryToSbyte() ?? return null out.add(small) } return out } List undo(short first, read List steps) { var readings = List { first } short value = first for step in steps { value += step readings.add(value) } return readings } void main() { var tenths = List { 215, 218, 221, 219, 190, 185 } var small = steps(read tenths) ?? panic("a step too large") print(small) print(undo(tenths[0], read small)) var wide = List { 30000, -30000 } print(steps(read wide) is null) } .fi .RE .PP .RS 4 .nf [3, 3, -2, -29, -5] [215, 218, 221, 219, 190, 185] true .fi .RE .PP Readings of a temperature in tenths of a degree change little from one to the next, so each step is kept in one byte instead of two. \fB(int)readings[i]\fR makes the difference an \fBint\fR: two \fBshort\fRs would give a \fBshort\fR, and 30000 - (-30000) would overflow before it is tested. \fBtryToSbyte\fR gives \fBnull\fR for a step that does not fit. In \fBvalue += step\fR the \fBsbyte\fR widens to a \fBshort\fR\&. .SS "The bits of a byte, with a sign" .RS 4 .nf void main() { var record = List { 0x05, 0xfb, 0x80, 0x7f } var axes = List() for b in read record { axes.add(b.wrappingToSbyte()) } print(axes) print(record[1].tryToSbyte() ?? 0) var back = List() for a in read axes { back.add(a.wrappingToByte()) } print(back == record) print($"{axes[1]:x2} {record[1]:x2}") } .fi .RE .PP .RS 4 .nf [5, -5, -128, 127] 0 true fb fb .fi .RE .PP A sensor that sends signed bytes, such as an accelerometer, gives them as bytes. \fBwrappingToSbyte\fR reads each as two's complement: 0xfb is -5 and 0x80 is -128. \fBtryToSbyte\fR asks a different question, whether the value 251 fits, and it does not. The bits are the same both ways, so the hex is too. .SS "One behaviour in every build" .RS 4 .nf void main() { sbyte gain = 100 print($"{gain.saturatingAdd(50)} {gain.wrappingAdd(50)} {gain.tryAdd(50) ?? 0}") sbyte cold = -100 print($"{cold.saturatingSubtract(50)} {cold.saturatingMultiply(2)}") print($"{sbyte.MinValue.unsignedAbs()} {sbyte.MinValue.tryNegate() ?? 0} {sbyte.MinValue.wrappingNegate()}") print($"{(300).saturatingToSbyte()} {(-300).saturatingToSbyte()} {(-2.7).saturatingToSbyte()}") } .fi .RE .PP .RS 4 .nf 127 -106 0 -128 -128 128 0 -128 127 -128 -2 .fi .RE .PP \fBsaturatingAdd\fR stops at 127, \fBwrappingAdd\fR goes round to -106, and \fBtryAdd\fR gives \fBnull\fR\&. -128 has no positive \fBsbyte\fR: \fBunsignedAbs\fR gives 128 as a \fBbyte\fR, \fBtryNegate\fR gives \fBnull\fR and \fBwrappingNegate\fR gives -128 again. \fBsaturatingToSbyte\fR takes any wider number, a fraction too, and cuts a fraction toward zero. .SS "A signed char of C" .RS 4 .nf extern { raw sbyte strrchr(read CString text, int c) } void main() { var path = CString.from("/tmp/notes.txt") ?? panic("a zero byte inside") var dot = unsafe strrchr(read path, (int)'.') if dot != null { sbyte first = unsafe dot[0] print(first) print(unsafe string.fromCString(dot) ?? "not UTF-8") } } .fi .RE .PP .RS 4 .nf 46 \&.txt .fi .RE .PP C's \fBchar\fR has a sign on many machines, and a function that gives a \fBchar*\fR can be declared with \fBraw sbyte\fR\&. Reading through the pointer gives an \fBsbyte\fR, here the code of \fB\&.\fR\&. \fBstring.fromCString\fR takes a \fBraw sbyte\fR as well as a \fBraw byte\fR, copies the bytes up to the zero and checks that they are UTF-8. .SS "Read from text" .RS 4 .nf void main() { for text in (List { "-12", "127", "128", "+5", "1_0" }) { match sbyte.parse(read text) { Ok(n) => print($"{text}: {n}") Error(e) => print($"{text}: {e}") } } print(sbyte.parseHex("7f") ?? 0) } .fi .RE .PP .RS 4 .nf -12: -12 127: 127 128: OutOfRange +5: 5 1_0: Invalid(1) 127 .fi .RE .PP \fBparse\fR reads a sign and digits; a number past an end is \fBOutOfRange\fR, and anything else is \fBInvalid\fR with the place where the text stops being a number. .SH "WHAT STOPS THE PROGRAM" .SS "A sum past 127" .RS 4 .nf sbyte total(read List steps) { sbyte sum = 0 for s in steps { sum += s } return sum } void main() { var steps = List { 100, 20, 10 } print(total(read steps)) } .fi .RE .PP .RS 4 .nf panic: overflow: 120 + 10 does not fit in sbyte --> example.sg:4:9 4 | sum += s | ^^^^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around .fi .RE .PP In a release build the sum wraps to -126. A sum of many \fBsbyte\fRs is kept in a wider type: with \fBint sum = 0\fR, each \fBsbyte\fR widens as it is added. .SS "A byte above 127, cast" .RS 4 .nf void main() { var record = List { 0x05, 0xfb } for b in read record { print((sbyte)b) } } .fi .RE .PP .RS 4 .nf 5 panic: value 251 does not fit in sbyte --> example.sg:4:15 4 | print((sbyte)b) | ^^^^^^^^ hint: `tryToSbyte()` gives `null` when the value does not fit, and `wrappingToSbyte()` cuts it to size .fi .RE .PP A cast keeps the value, and 251 is not an \fBsbyte\fR\&. To read the bits with a sign, \fBb.wrappingToSbyte()\fR gives -5 in every build. .SH "WHAT THE COMPILER REFUSES" .SS "A number that does not fit" .RS 4 .nf void main() { sbyte temperature = 130 print(temperature) } .fi .RE .PP .RS 4 .nf error: `130` does not fit in sbyte --> example.sg:2:25 2 | sbyte temperature = 130 | ^^^ .fi .RE .PP \fBsbyte t = \-129\fR and \fB(sbyte)200\fR are refused the same way. .SS "An sbyte and a byte" .RS 4 .nf void main() { sbyte offset = -3 byte level = 200 print(level + offset) } .fi .RE .PP .RS 4 .nf error: `byte` and `sbyte` do not mix: neither holds every value of the other --> example.sg:4:11 4 | print(level + offset) | ^^^^^^^^^^^^^^ hint: `short` holds both: `(short)offset` .fi .RE .SS "A byte where an sbyte goes" .RS 4 .nf void main() { var data = List { 0x05, 0xfb } sbyte s = data[1] print(s) } .fi .RE .PP .RS 4 .nf error: expected `sbyte`, found `byte` --> example.sg:3:15 3 | sbyte s = data[1] | ^^^^^^^ hint: convert with `(sbyte)data[1]`; a value that does not fit panics, or is cut to size in a release build .fi .RE .PP Where the byte holds a signed value, \fBdata[1].wrappingToSbyte()\fR reads its bits. .SS "A number past the ends, beside an sbyte" .RS 4 .nf void main() { sbyte reading = -5 print(reading < 200) } .fi .RE .PP .RS 4 .nf error: `200` does not fit in sbyte --> example.sg:3:21 3 | print(reading < 200) | ^^^ hint: compare in `int`: `(int)reading < 200` .fi .RE .PP The number takes the type of the other side, and 200 is not an \fBsbyte\fR\&. \fB(int)reading < 200\fR compares two \fBint\fRs. .SH "SEE ALSO" .BR sg-byte (7), .BR sg-short (7), .BR sg-int (7), .BR sg-char (7), .BR sg-raw (7) .PP https://sg\-lang.org/reference/sbyte.html