Primitive type auto-imported

byte

type byte
Source: compiler/src/builtin_docs/types.hy

An unsigned 8-bit integer in 0..=255.

There is no byte literal syntax. An integer literal in 0..=255 becomes a byte where a byte is expected (an annotated let, an argument, a return, a Vec<byte> element), and so does a string literal that encodes to exactly one UTF-8 byte (let c: byte = "A";). A literal outside the range is a compile-time error. Elsewhere, write n as byte.

byte arithmetic wraps modulo 256: +, -, *, **, << and unary - keep the low 8 bits (200 + 100 is 44, 0 - 1 is 255), and each such operator warns E0130. Convert to int first (b as int) for the exact result. / and % by zero panic with division by zero.

Casts with as: byte as int is exact; int as byte keeps the low 8 bits. There is no direct cast to or from float.

byte implements Eq, Show, Shl, Shr, BitAnd, BitOr and BitXor, and supports <, <=, >, >=. It is not Num, BitNot or Integral. Byte buffers are Vec<byte>; see string::to_bytes.

Examples#

use tests::assert;
let a: byte = 65;
let c: byte = "A";
assert!(a == c);
assert!(a as int + 200 == 265);
assert!((a | 32 as byte) == 97 as byte);
let n = 257;
assert!(n as byte == 1 as byte);

See also#

↑ ↓ navigate ↵ open