Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,961 questions

51,903 answers

573 users

How to create a bitset in PHP

1 Answer

0 votes
$bitset = new SplFixedArray(16); // Create a bitset with 16 bits

// Initialize all bits to 0
for ($i = 0; $i < $bitset->getSize(); $i++) {
    $bitset[$i] = 0;
}

// Set a bit
$bitset[3] = 1;

// Check a bit
if ($bitset[3] == 1) {
    echo "Bit 3 is set.\n";
} else {
    echo "Bit 3 is not set.\n";
}

$reversedFixedArray = ConvertSplFixedArrayToTegularArray($bitset);

// Print the reversed SplFixedArray
foreach ($reversedFixedArray as $value) {
    echo $value . " ";
}

// Convert SplFixedArray to a regular array
function ConvertSplFixedArrayToTegularArray($fixedArray) {
    $array = $fixedArray->toArray();

    // Reverse the array
    $reversedArray = array_reverse($array);

    // Convert back to SplFixedArray
    $reversedFixedArray = SplFixedArray::fromArray($reversedArray);
    
    
    return $reversedFixedArray;
}



/*
run:

Bit 3 is set.
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 

*/

 



answered May 7, 2025 by avibootz

Related questions

1 answer 37 views
1 answer 106 views
106 views asked May 7, 2025 by avibootz
1 answer 98 views
1 answer 103 views
103 views asked May 7, 2025 by avibootz
2 answers 107 views
2 answers 83 views
2 answers 94 views
...