bits
Table of Contents
Traits
- BitFlagTrait
- Trait providing common methods for bitmask flag enumerations.
Functions
- countFlags() : int
- Counts the number of active flags (bits set to 1) in a bitmask.
- hasAllFlags() : bool
- Checks whether **all** specified flags are set in a bitmask.
- hasFlag() : bool
- Checks whether a specific flag is set in a bitmask.
- isValidMask() : bool
- Validates that a bitmask contains only allowed flags.
- setFlag() : int
- Sets a specific flag in a bitmask.
- toggleFlag() : int
- Toggles a specific flag in a bitmask.
- unsetFlag() : int
- Unsets (removes) a specific flag from a bitmask.
Functions
countFlags()
Counts the number of active flags (bits set to 1) in a bitmask.
countFlags(int $mask) : int
This function is useful when you want to know how many flags are currently active.
Parameters
- $mask : int
-
The bitmask to analyze.
Tags
Return values
int —The number of bits set to 1 in the mask.
hasAllFlags()
Checks whether **all** specified flags are set in a bitmask.
hasAllFlags(int $mask, int $flags) : bool
This function is useful when you want to ensure that a set of flags are all active within a bitmask.
Parameters
- $mask : int
-
The original bitmask, potentially containing multiple flags combined with
|. - $flags : int
-
The flags to check (can be a single flag or multiple combined with
|).
Tags
Return values
bool —Returns true if all the given flags are present in the mask, false otherwise.
hasFlag()
Checks whether a specific flag is set in a bitmask.
hasFlag(int $mask, int $flag) : bool
This function is useful when working with bitwise flags.
You can combine multiple flags using the bitwise OR operator (|)
and then check if a particular flag is present in the combined mask.
Parameters
- $mask : int
-
The bitmask value, potentially containing multiple flags combined with
|. - $flag : int
-
The specific flag to check for in the mask.
Tags
Return values
bool —Returns true if the given flag is present in the mask, false otherwise.
isValidMask()
Validates that a bitmask contains only allowed flags.
isValidMask(int $mask, int $allowed) : bool
Parameters
- $mask : int
-
The bitmask to validate.
- $allowed : int
-
A bitmask of all valid flags.
Tags
Return values
bool —Returns true if all bits in $mask are present in $allowed, false otherwise.
setFlag()
Sets a specific flag in a bitmask.
setFlag(int $mask, int $flag) : int
Parameters
- $mask : int
-
The original bitmask.
- $flag : int
-
The flag to set.
Tags
Return values
int —The new bitmask with the flag set.
toggleFlag()
Toggles a specific flag in a bitmask.
toggleFlag(int $mask, int $flag) : int
If the flag is present, it will be removed; if absent, it will be added.
Parameters
- $mask : int
-
The original bitmask.
- $flag : int
-
The flag to toggle.
Tags
Return values
int —The new bitmask with the flag toggled.
unsetFlag()
Unsets (removes) a specific flag from a bitmask.
unsetFlag(int $mask, int $flag) : int
Parameters
- $mask : int
-
The original bitmask.
- $flag : int
-
The flag to remove.
Tags
Return values
int —The new bitmask with the flag removed.