bitmasks: Bitmasks for efficient storing of boolean flags

[ bsd3, data, library ] [ Propose Tags ] [ Report a vulnerability ]

Please see the README on GitHub at https://github.com/AliceRixte/bitmasks#readme


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0, 0.0.1
Change log CHANGELOG.md
Dependencies base (>=4.18 && <5) [details]
License BSD-3-Clause
Copyright (c) Alice Rixte 2025
Author Alice Rixte
Maintainer alice.rixte@u-bordeaux.fr
Uploaded by AliceRixte at 2026-03-09T20:43:13Z
Category data
Home page https://github.com/AliceRixte/bitmasks#readme
Bug tracker https://github.com/AliceRixte/bitmasks/issues
Source repo head: git clone https://github.com/AliceRixte/bitmasks
Distributions NixOS:0
Downloads 20 total (3 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for bitmasks-0.0.1

[back to package description]

Bitmasks

Haskell BSD3 License Hackage Nightly LTS

Bitmasks for efficient storing of boolean flags.

Alternatives

You might prefer the bitset package, that uses a similar implementation to this library. The main difference between bitset and bitmasks is that, contrarily to bitset, bitmasks is intended to be imported non qualified.

Usage

Define your flags as an enumeration:

import Data.Word
import Data.Bitmask

data PizzaTopping =
    Cheese
  | Mushrooms
  | Pineapple
  | Ham
  deriving (Show, Eq, Bounded, Enum)

-- We only need 8 bits since there are only 4 toppings
type PizzaMask = Bitmask8 PizzaTopping

Creating bitmasks

-- A Margherita pizza (cheese only)
margherita :: PizzaMask
margherita = fromFlags [Cheese]

veggie :: PizzaMask
veggie = fromExceptFlags [Ham]

Access and modify flags

Use getFlag to check if a pizza has a specific topping:

>>> getFlag Cheese funghi
True
>>> getFlag Pineapple funghi
False

Add toppings to a pizza:

>>> hawaiian = addFlags [Pineapple, Ham] margherita
>>> getFlags [Pineapple, Mushroom] hawaiian
True

Make any pizza vegetarian (bitwise AND):

>>> veggieHawaiian = veggie .&. hawaiian
>>> getFlag Ham veggieHawaiian

Toggle the toppinsertFlages, topping toggling should be a thing):

>>> funghi = flipFlags [Pineapple, Mushroom] veggieHawaiian
>>> toFlags funghi
[Cheese,Mushrooms]

Remove a topping:

>>> margherita == deleteFlag Mushroom funghi
True

Convert to lists

-- Get all toppings as a list
>>> toFlags funghi
[Cheese,Mushrooms]
>>> toFlags hawaiian
[Cheese,Pineapple,Ham]

-- Convert to association lists
>>> toFlagsBool funghi
[(Cheese,True),(Mushrooms,True),(Pineapple,False),(Ham,False)]