Standard Library
class int8
A signed 8-bit integer: an int8 holds -128 to 127 and is stored unboxed.
since 0.1.0-alpha.1linuxwindowswasm
Overview
Arithmetic between int8 values wraps around in two's complement at 8 bits instead of failing: 127 + 1 is -128 and -128 - 1 is 127. Even -128 / -1, whose true result 128 does not fit, gives -128. >> keeps the sign. A bare integer literal next to an int8 operand counts as an int8, while an int variable makes the result an int. Division or remainder by zero throws a RuntimeException. A literal assigned to an int8 is checked when the program is compiled.
Converting an int with int.toInt8() does not wrap, it throws when the value is outside -128..127. toInt() converts back and never fails.
Examples
Wrapping and range
int8 top = 127;
int8 one = 1;
console.writeln(top + one);
int8 bottom = -128;
console.writeln(bottom - one);
console.writeln(bottom.abs());
int8 minusOne = -1;
console.writeln(bottom / minusOne);
try {
console.writeln(200.toInt8());
} catch (RuntimeException e) {
console.writeln("caught: ${e.message}");
}
-128
127
-128
-128
caught: toInt8: value out of range (-128..127)
Methods
abs
@__narrowShared("abs")
abs() -> int8Return the absolute value.
Two's complement has one more negative value than positive ones, so the absolute value of -128, the smallest int8, does not fit and abs returns -128 unchanged.
Returns
this when it is zero or positive, otherwise -this, as a int8.
Examples
int8 n = -5;
console.writeln(n.abs());
int8 bottom = -128;
console.writeln(bottom.abs());
int8 top = 127;
console.writeln(top.abs());
5
-128
127
clamp
@__narrowShared("clamp")
clamp(int8 lo, int8 hi) -> int8Limit the int8 to the range from lo to hi.
Parameters
- lo
- The smallest value to return.
- hi
- The largest value to return.
Returns
lo if this is below it, hi if this is above it, otherwise this.
Throws
RuntimeException- when
lois greater thanhi.
Examples
int8 lo = -10;
int8 hi = 10;
int8 top = 127;
int8 bottom = -128;
int8 mid = 5;
console.writeln(top.clamp(lo, hi));
console.writeln(bottom.clamp(lo, hi));
console.writeln(mid.clamp(lo, hi));
try {
console.writeln(mid.clamp(hi, lo));
} catch (RuntimeException e) {
console.writeln("caught: ${e.message}");
}
10
-10
5
caught: clamp: lo > hi
max
@__narrowShared("max")
max(int8 other) -> int8Return the larger of two int8 values.
Parameters
- other
- The value to compare with.
Returns
this if it is greater than other, otherwise other.
Examples
int8 top = 127;
int8 bottom = -128;
console.writeln(top.max(bottom));
console.writeln(bottom.max(bottom));
127
-128
See also: min
min
@__narrowShared("min")
min(int8 other) -> int8Return the smaller of two int8 values.
Parameters
- other
- The value to compare with.
Returns
this if it is less than other, otherwise other.
Examples
int8 top = 127;
int8 bottom = -128;
console.writeln(top.min(bottom));
console.writeln(top.min(top));
-128
127
See also: max
pow
@__narrowShared("pow")
pow(int8 e) -> int8Raise the int8 to a power, wrapping at 8 bits.
The multiplication is repeated squaring and wraps at 8 bits at every step, so the result is the true power folded into -128..127: 3 to the 5th is -13 (243 minus 256) and 2 to the 7th is -128. A negative exponent has no integer result and gives 0, as with int.pow.
Parameters
- e
- The exponent.
Returns
this to the power e, as a int8.
Examples
int8 three = 3;
int8 five = 5;
int8 six = 6;
console.writeln(three.pow(five));
console.writeln(three.pow(six));
int8 two = 2;
int8 seven = 7;
console.writeln(two.pow(seven));
int8 minusOne = -1;
console.writeln(two.pow(minusOne));
-13
-39
-128
0
sign
@__narrowShared("sign")
sign() -> int8Return the sign of the value.
Returns
-1 if this is negative, 0 if it is zero, and 1 if it is positive, as a int8.
Examples
int8 bottom = -128;
int8 zero = 0;
int8 top = 127;
console.writeln(bottom.sign());
console.writeln(zero.sign());
console.writeln(top.sign());
-1
0
1
toFloat
toFloat() -> floatConvert the int8 to a float.
The conversion is exact: every value of a int8 is representable as a float.
Returns
The value as a float.
Examples
int8 top = 127;
console.writeln(top.toFloat());
console.writeln(top.toFloat() / 2.0);
127.000000
63.500000
toHex
toHex() -> stringFormat the int8 in hexadecimal.
The digits are lowercase with no 0x prefix and no padding. A negative value gets a leading - before the digits of its magnitude, so -128 formats as -80 and -13 as -d.
Returns
The hexadecimal digits of this.
Examples
int8 top = 127;
console.writeln(top.toHex());
int8 bottom = -128;
console.writeln(bottom.toHex());
int8 minusThirteen = -13;
console.writeln(minusThirteen.toHex());
7f
-80
-d
See also: toString
toInt
toInt() -> intConvert the int8 to an int.
The value is unchanged; every int8 value fits in an int, so this conversion never throws. Convert back with int.toInt8, which throws when the value is outside -128..127.
Returns
The same number as an int.
Examples
int8 m = -128;
console.writeln(m.toInt() - 1);
console.writeln(m - 1);
-129
127
See also: toInt8
toString
toString() -> stringFormat the int8 as decimal text.
A negative value gets a leading -. The toString(int radix) overload formats in another base.
Returns
The decimal digits of this.
Examples
int8 n = -128;
console.writeln(n.toString());
console.writeln("n = " + n.toString());
console.writeln(n.toString(2));
console.writeln(n.toString(16));
-128
n = -128
-10000000
-80
toString(int radix) -> stringFormat the int8 as text in the given base.
Digits above 9 are lowercase letters (a is ten). A negative value gets a leading - before the digits of its magnitude. This formats the same number as toInt().toString(radix).
Parameters
- radix
- The base, from 2 to 36 inclusive.
Returns
The digits of this in base radix.
Throws
RuntimeException- when
radixis outside 2..36.
See also: toHex