Attention
TYPO3 v11 has reached end-of-life as of October 31th 2024 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v11 here: TYPO3 ELTS.
How to use bitsets
Bitsets are used to handle boolean flags efficiently.
The class 
        \TYPO3\ provides a TYPO3 implementation of
a bitset. It can be used standalone and accessed from the outside, but we
recommend creating specific bitset classes that extend the TYPO3
        Bit class.
The functionality is best described by an example:
<?php
declare(strict_types=1);
use TYPO3\CMS\Core\Type\BitSet;
define('PERMISSIONS_NONE', 0b0); // 0
define('PERMISSIONS_PAGE_SHOW', 0b1); // 1
define('PERMISSIONS_PAGE_EDIT', 0b10); // 2
define('PERMISSIONS_PAGE_DELETE', 0b100); // 4
define('PERMISSIONS_PAGE_NEW', 0b1000); // 8
define('PERMISSIONS_CONTENT_EDIT', 0b10000); // 16
define('PERMISSIONS_ALL', 0b11111); // 31
$bitSet = new BitSet(PERMISSIONS_PAGE_SHOW | PERMISSIONS_PAGE_NEW);
$bitSet->get(PERMISSIONS_PAGE_SHOW); // true
$bitSet->get(PERMISSIONS_CONTENT_EDIT); // false
The example above uses global constants. Implementing that via an extended bitset class makes it clearer and easier to use:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Bitmask;
use TYPO3\CMS\Core\Type\BitSet;
final class Permissions extends BitSet
{
    public const NONE = 0b0; // 0
    public const PAGE_SHOW = 0b1; // 1
    public const PAGE_EDIT = 0b10; // 2
    public const PAGE_DELETE = 0b100; // 4
    public const PAGE_NEW = 0b1000; // 8
    public const CONTENT_EDIT = 0b10000; // 16
    public const ALL = 0b11111; // 31
    public function hasPermission(int $permission): bool
    {
        return $this->get($permission);
    }
    public function hasAllPermissions(): bool
    {
        return $this->get(self::ALL);
    }
    public function allow(int $permission): void
    {
        $this->set($permission);
    }
}
Then use your custom bitset class: