Bool - chung-leong/zigar GitHub Wiki

The bool type in Zig corresponds exactly to boolean in JavaScript. It's used for storing variables that can be either true or false.

pub fn not(value: bool) bool {
    return !value;
}
import { not } from './bool-example-1.zig';

console.log(not(true));
console.log(not(false));
false
true

In packed struct

A bool typically takes up 1 byte. Within a packed struct it uses only a single bit. This allows you to store a large number of states very efficiently.

pub const UserPreferences = packed struct {
    option1: bool = false,
    option2: bool = false,
    option3: bool = false,
    option4: bool = false,
    option5: bool = false,
    option6: bool = false,
    option7: bool = false,
    option8: bool = false,
    option9: bool = false,
    option10: bool = false,
    option11: bool = false,
    option12: bool = false,
    option13: bool = false,
    option14: bool = false,
    option15: bool = false,
    option16: bool = false,
};
import { UserPreferences } from './bool-example-2.zig';

const pref = new UserPreferences({ option8: true });
console.log(pref.valueOf());
console.log(`size = ${pref.dataView.byteLength}`);
{
  option1: false,
  option2: false,
  option3: false,
  option4: false,
  option5: false,
  option6: false,
  option7: false,
  option8: true,
  option9: false,
  option10: false,
  option11: false,
  option12: false,
  option13: false,
  option14: false,
  option15: false,
  option16: false
}
size = 2