Booleans
A boolean holds one of two values, true or false. Comparisons produce booleans, and if, while and the logical operators consume them.
The boolean types
| Type | Bits | Bytes | Values |
|---|---|---|---|
bool8 | 8 | 1 | false, true |
bool16 | 16 | 2 | false, true |
bool32 | 32 | 4 | false, true |
bool64 | 64 | 8 | false, true |
bool128 | 128 | 16 | false, true |
bool256 | 256 | 32 | false, true |
bool512 | 512 | 64 | false, true |
bool is a built-in alias of bool8, and the everyday spelling. The wider types hold the same two values in more storage; they exist to match the layout of foreign code, such as the four-byte BOOL of the Windows API, which is a bool32 in Rux. A compiler message names the canonical type, so a mistake with a bool usually reports bool8; the message for a condition spells it bool on both sides, as in condition for 'if' must have type 'bool', but found 'bool?'.
Associated constants
Imported from Core, each boolean type has Bits and Bytes (both uint): bool32::Bits is 32 and bool32::Bytes is 4; bool512::Bytes is 64.
Literals
true and false are the only boolean literals. Both are keywords, and their type is bool.
let ready = true;
let finished: bool = false;
let wide: bool32 = ready;
Operations
| Operator | Meaning |
|---|---|
!a | Not |
a && b | And; b is evaluated only when a is true |
a || b | Or; b is evaluated only when a is false |
a & b | And, always evaluating both operands |
a | b | Or, always evaluating both operands |
a ^ b | Exclusive or: true when exactly one operand is true |
a == b, a != b | Equality |
let a = true;
let b = false;
let both = a && b; // false
let either = a || b; // true
let differ = a ^ b; // true
&& binds more tightly than ||, so a || b && c is a || (b && c). See Logical for short-circuiting and Expressions for precedence.
Conditions
A condition must be a boolean. A number is not treated as one:
if 1 { … }
error: condition for 'if' must have type 'bool', but found 'int'
let flag = !5;
error: operator '!' requires a bool operand, but found 'int'
Write the comparison you mean, count != 0. Comparing a boolean with a literal, ready == true, is legal but says nothing ready does not.
Conversions
Every boolean width converts implicitly to every other, since all of them hold the same two values:
let flag: bool = true;
let wide: bool32 = flag;
let back: bool = wide;
Between booleans and other types, conversion is written with as:
| Conversion | Result |
|---|---|
| Boolean to an integer or float | 1 for true, 0 for false |
| Integer, float or character to a boolean | false for zero, true for anything else |
let one = true as int; // 1
let zero = false as uint8; // 0
let set = 256 as bool; // true
let some = 0.5 as bool; // true
A cast to a boolean tests the whole value for zero; it does not keep low bits. 256 as bool is true even though the low byte of 256 is zero, and the result reads back as exactly 1.
Without as, a number is never a boolean: let on: bool = 1; is error: cannot assign 'int' to 'bool8', and let n: int = true; is error: cannot assign 'bool8' to 'int'.
See also
Floating-Point
The eight floating-point widths in Rux, from float8 to float512: formats, constants, special values, arithmetic, how each width is computed, and conversions.
Characters
Rux's character types: char8 and char16 code units, char32 to char512 Unicode scalar values, literals, comparison and conversions.