Bitmask
A bitmask value object for PHP
Install / Use
/learn @dotgksh/BitmaskREADME
gksh/bitmask
A simple way to use bitmask and bitwise operations in PHP.
Installation
Requires PHP 8.2+
composer require gksh/bitmask
Usage
Streamline flag handling by encoding boolean options into simple integers through bitmasking.
Please see ide.php for full example and playground for more.
use Gksh\Bitmask\Bitmask;
enum Panel: int
{
case Project = 1;
case Terminal = 2;
case SourceControl = 4;
case Extensions = 8;
}
class Ide
{
public Bitmask $panels;
public function __construct()
{
$this->panels = Bitmask::tiny(); // 8-bit
}
public function togglePanel(Panel $panel): self
{
$this->panels = $this->panels->toggle($panel);
return $this;
}
}
$ide = (new Ide())
->togglePanel(Panel::Project)
->togglePanel(Panel::Terminal);
$ide->panels->has(Panel::Terminal); // true
$ide->panels->has(Panel::Extensions); // false
Features
- Immutable: Operations return new instances, original unchanged
- Enum support: Pass
BackedEnumdirectly — no->valueextraction needed - Size variants:
tiny()(8-bit),small()(16-bit),medium()(24-bit),make()(32-bit default)
Factory Methods
Bitmask::make() // 32-bit (default)
Bitmask::tiny() // 8-bit, for TINYINT columns
Bitmask::small() // 16-bit, for SMALLINT columns
Bitmask::medium() // 24-bit, for MEDIUMINT columns
Operations
$mask = Bitmask::tiny()
->set(Flag::A) // Set a flag
->unset(Flag::B) // Unset a flag
->toggle(Flag::C); // Toggle a flag
$mask->has(Flag::A); // Check if flag is set
$mask->value(); // Get integer value
$mask->size(); // Get Size enum
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
Related Skills
node-connect
342.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
85.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
342.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
342.5kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
