How to order two bools?

Is there some existing function to order two booleans?

fn where_is_it(a:  bool, b: bool) enum { .lt, .eq, .gt}

I cannot find anything specific for bool in the standard library.
std.math.order does not work for bool, because operators < and > are not defined for bool.
The following works by converting false to 0 and true to 1:

fn order(x: bool, y: bool) std.math.Order {
    return std.math.order(@intFromBool(x), @intFromBool(y));

Interesting philosophical question. How would you define the order? Is true superior to false?

Depending on the circumstance, you can define min, max, and equal using typical boolean functions.

  • min a, b: a and b… true and false → false
  • max a, b: a or b… true or false → true

Same can be done with eql and XNOR, but we should probably open a new topic about this.


I have used this once for sorting with a lessThan function:

fn boolLessThan(lhs: bool, rhs: bool) bool {
    // true if lhs is false and rhs true
    // every other case false
    return !lhs and rhs;
1 Like

u1 and bool are isometric. They should coerce seemlessly in the language. APL treats bools as u1 to the point that it doesn’t even have boolean operations just min and max. (I don’t even think bools are needed in zig).

min = and
max = or

without overflow + is xor even.

That’s exactly how the compiler will (should) compile it down if you tried to use < on bools with intfrombool

That’s not true in Zig semantics: and short circuits, while min always evaluates all arguments

not for just bools in release. but i meant that in a general context as in if you got rid of bool you could still do & and | for min and max.

Based on archaeological evidence. We know that true is superior to false thanks to an ancient code fragment dating from the time of the last crusade:

while (grail) {} else @panic("What is happening to me?");