typestar

bit_set in Odin

Market days packed into bits: set literals, +=, membership, card, and complement.

package main

import "core:fmt"

Day :: enum {Mon, Tue, Wed, Thu, Fri, Sat, Sun}

main :: proc() {
    // A bit_set packs enum membership into an integer's bits.
    market_days: bit_set[Day] = {.Tue, .Sat}
    market_days += {.Sun}

    fmt.println("open on Tuesday?", .Tue in market_days)
    fmt.println("open on Monday? ", .Mon in market_days)
    fmt.println("days open:", card(market_days))
    fmt.println("closed:", ~market_days)
}

How it works

  1. bit_set[Day] stores enum membership in an integer's bits.
  2. {.Tue, .Sat} is a set literal; += {.Sun} unions one more in.
  3. in tests membership, card counts, and ~ flips to the closed days.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
411
Tokens
102
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 94 seconds.

Type this snippet

Step 3 of 3 in Types & generics, step 21 of 27 in Language basics.

← Previous Next →