Oihana PHP

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
example
use function oihana\core\bits\countFlags;

const FLAG_A = 1 << 0 ; // 1
const FLAG_B = 1 << 1 ; // 2
const FLAG_C = 1 << 2 ; // 4

$mask = FLAG_A | FLAG_B;  // 3
echo countFlags($mask);   // Outputs: 2

$mask = FLAG_A | FLAG_B | FLAG_C ; // 7
echo countFlags( $mask ) ; // Outputs: 3

$mask = 0; // no flags
echo countFlags($mask); // Outputs: 0
author

Marc Alcaraz (ekameleon)

since
1.0.7
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
example
use function oihana\core\bits\hasAllFlags;

const FLAG_A = 1 << 0; // 1
const FLAG_B = 1 << 1; // 2
const FLAG_C = 1 << 2; // 4

$mask = FLAG_A | FLAG_B ; // 3

hasAllFlags( $mask , FLAG_A ) ;          // true
hasAllFlags( $mask , FLAG_A | FLAG_B ) ; // true
hasAllFlags( $mask , FLAG_A | FLAG_C ) ; // false
author

Marc Alcaraz (ekameleon)

since
1.0.7
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
example
use function oihana\core\bits\hasFlag;

const FLAG_A = 1 << 0;
const FLAG_B = 1 << 1;

$mask = FLAG_A | FLAG_B;

hasFlag($mask, FLAG_A); // true
hasFlag($mask, FLAG_B); // true
hasFlag($mask, 1 << 2); // false
author

Marc Alcaraz (ekameleon)

since
1.0.7
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
example
use function oihana\core\bits\isValidMask;

const FLAG_A = 1 << 0; // 1
const FLAG_B = 1 << 1; // 2
const FLAG_C = 1 << 2; // 4
const ALL_FLAGS = FLAG_A | FLAG_B | FLAG_C; // 7

$mask = FLAG_A | FLAG_B;
isValidMask( $mask , ALL_FLAGS ) ; // true

$mask = FLAG_A | FLAG_B | (1 << 5);
isValidMask( $mask , ALL_FLAGS ) ; // false, bit 5 is invalid
author

Marc Alcaraz (ekameleon)

since
1.0.7
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
example
use function oihana\core\bits\setFlag;

const FLAG_A = 1 << 0; // 1
const FLAG_B = 1 << 1; // 2

$mask = FLAG_A;          // 1
$mask = setFlag( $mask  , FLAG_B ) ;
// $mask is now 3 (FLAG_A | FLAG_B)
author

Marc Alcaraz (ekameleon)

since
1.0.7
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
example
use function oihana\core\bits\toggleFlag;

const FLAG_A = 1 << 0; // 1
const FLAG_B = 1 << 1; // 2

$mask = FLAG_A;        // 1
$mask = toggleFlag($mask, FLAG_B);
// $mask is now 3 (FLAG_A | FLAG_B)

$mask = toggleFlag($mask, FLAG_A);
// $mask is now 2 (FLAG_B only)
author

Marc Alcaraz (ekameleon)

since
1.0.7
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
example
use function oihana\core\bits\unsetFlag;

const FLAG_A = 1 << 0; // 1
const FLAG_B = 1 << 1; // 2

$mask = FLAG_A | FLAG_B; // 3
$mask = unsetFlag($mask, FLAG_B);
// $mask is now 1 (FLAG_A only)
author

Marc Alcaraz (ekameleon)

since
1.0.7
Return values
int

The new bitmask with the flag removed.


        
On this page

Search results