<?php

namespace monoformat {

function isBitSet(int $value, int $bit) {
    if ($bit < 0 || $bit >= (PHP_INT_SIZE * 8)) {
        return false;
    }
    $mask = 1 << $bit;
    return ($value & $mask) == $mask;
}

function setBit(int &$value, int $bit) {
    if ($bit < 0 || $bit >= (PHP_INT_SIZE * 8)) {
        return;
    }
    $mask = 1 << $bit;
    $value |= $mask;
}

function unsetBit(int &$value, int $bit) {
    if ($bit < 0 || $bit >= (PHP_INT_SIZE * 8)) {
        return;
    }
    $mask = 1 << $bit;
    $value &= ~$mask;
}

function maybeSetBit(int &$value, int $bit, int $set) {
    if ($bit < 0 || $bit >= (PHP_INT_SIZE * 8)) {
        return;
    }
    $mask = 1 << $bit;
    if ($set) {
        $value |= $mask;
    } else {
        $value &= ~$mask;
    }
}

} // namespace monoformat

?>
