Integers
Cloth provides eight fixed-width integer types: four signed and four unsigned. Every integer type has a precise, platform-independent bit width and value range.
Signed integers
Signed integers are stored in two’s-complement form. The most significant bit is the sign bit; the remaining bits encode magnitude.
| Type | Bits | Minimum | Maximum |
|---|---|---|---|
i8 | 8 | −128 | 127 |
i16 | 16 | −32,768 | 32,767 |
i32 | 32 | −2,147,483,648 | 2,147,483,647 |
i64 | 64 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
Unsigned integers
Unsigned integers represent only non-negative values. All bits encode magnitude.
| Type | Bits | Minimum | Maximum |
|---|---|---|---|
u8 | 8 | 0 | 255 |
u16 | 16 | 0 | 65,535 |
u32 | 32 | 0 | 4,294,967,295 |
u64 | 64 | 0 | 18,446,744,073,709,551,615 |
Aliases
The following names are aliases for fixed-width integer types. They exist for readability and for compatibility with naming conventions from other languages.
| Alias | Notes |
|---|---|
int | A signed integer; the standard convenience name. |
uint | An unsigned integer. |
unsigned | Synonym for uint. |
short | A short signed integer. |
long | A long signed integer. |
byte | An 8-bit value (see Boolean, Char, and Byte). |
Literals
Integer literals are written as decimal digit sequences. They take the smallest type that can represent them, with i32 as the default.
let x = 1;
let y = 1000000;Arithmetic
The standard arithmetic operators apply to all integer types: +, -, *, /, %. Comparison operators (==, !=, <, <=, >, >=) and bitwise operators (&, |, ^, ~, <<, >>) are also defined.
Operands of binary arithmetic operators must have the same type. Mixing differently sized or differently signed integers requires an explicit cast (see Operators).
Use in declarations
Integer types appear in field, parameter, variable, and return-type positions:
private i32 v;
public Foo(i32 x) {
v = x;
}
public func get(): i32 {
return v;
}I/O
The standard library provides println overloads for i32, i64, u32, and u64:
import cloth.io.Out::{ println };
println(42);The full set of overloads is described in cloth.io.Out.