(* A BOOL converts to each integer type, false to 0 and true to 1 (report section 6.7): a variable, a comparison, a condition made of and and or, and a constant, in a constant too; and 0 - U64(a = b), a mask of all ones or none, chooses with no if. *) module BoolInt; import Out; const One = INT(true) + I8(false); (* 1 *) Mask = 0 - U64(1 < 2); (* all ones *) var t, f: BOOL; (* x where a = b, else y, by a mask and no jump *) proc choose(a, b, x, y: U64): U64; var m: U64; begin m := 0 - U64(a = b); return (x & m) | (y & ~m); end; proc main*; var a, b: INT; u8: U8; i16: I16; u32: U32; i64: I64; uint: UINT; begin t := true; a := 3; b := 4; u8 := U8(t); i16 := I16(f); u32 := U32(t); i64 := I64(t); uint := UINT(f); Out.int(I64(u8)); Out.str(" "); Out.int(I64(i16)); Out.str(" "); Out.int(I64(u32)); Out.str(" "); Out.int(i64); Out.str(" "); Out.int(I64(uint)); Out.ln; Out.int(INT(a < b)); Out.str(" "); Out.int(INT(a = b)); Out.str(" "); Out.int(I64(I8(a < b) - I8(a > b))); Out.ln; Out.int(INT((a < b) and (b < 10))); Out.str(" "); Out.int(INT((a > b) or f)); Out.str(" "); Out.int(INT(not t)); Out.ln; Out.int(One); Out.str(" "); Out.hex(Mask, 16); Out.ln; Out.hex(0 - U64(a = b), 16); Out.str(" "); Out.hex(0 - U64(a # b), 16); Out.ln; Out.int(I64(choose(5, 5, 10, 20))); Out.str(" "); Out.int(I64(choose(5, 6, 10, 20))); Out.ln; (* counting what holds *) Out.int(INT(a > 0) + INT(b > 0) + INT(a > b)); Out.ln; end; end BoolInt.