PHP code example of vinicciusguedes / laravel-bitwise

1. Go to this page and download the library: Download vinicciusguedes/laravel-bitwise library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vinicciusguedes / laravel-bitwise example snippets


$currentValue = 5; // 0101 em binário
$bitToAdd = 2;    // 0010 em binário
$newValue = Bitwise::addBit($currentValue, $bitToAdd); // 7 (0111 em binário)

$currentValue = 5; // 0101 em binário
$bitToAdd = [2, 4]; 
$newValue = Bitwise::addBits($currentValue, $bitToAdd); // 7 (0111 em binário)

$currentValue = 7; // 0111 em binário
$bitToRemove = 2;  // 0010 em binário
$newValue = Bitwise::removeBit($currentValue, $bitToRemove); // 5 (0101 em binário)

$currentValue = 15; // 1111 em binário
$bitToRemove = [1, 4];
$newValue = Bitwise::removeBits($currentValue, $bitToRemove); // 10 (1010 em binário)

$currentValue = 5;  // 0101 em binário
$bitToCheck = 4;    // 0100 em binário
$isActive = Bitwise::hasBit($currentValue, $bitToCheck); // true

$currentValue = 15; // 1111 em binário
$bitsArray = [1, 2, 4];
$allBitsActive = Bitwise::hasAllBits($currentValue, $bitsArray); // true

$value = 7;  // 0111 em binário
$activeBits = Bitwise::getActiveBits($value); // [1, 2, 4]

$bits = [1, 2, 4];
$sum = Bitwise::sumActiveBits($bits); // 7

$bitsArray = [1, 2];
$newArray = Bitwise::addBitInArray($bitsArray, 4); // [1, 2, 4]

$bitValue = 7;  // 0111 em binário
$bitsToCheck = [1, 2, 4];
$results = Bitwise::hasBitsInArray($bitValue, $bitsToCheck);
// [1 => true, 2 => true, 4 => true]

$bitsToCheck = [4 => 4, 2 => 2, 1 => 1];
$results = Bitwise::sortBitsByKey($bitsToCheck);
// [1 => 1, 2 => 2, 4 => 4]

$bitsToCheck = [4,2,1];
$results = Bitwise::sortBitsByValue($bitsToCheck);
// [2 => 1, 1 => 2, 0 => 4]

$number = 5;
$results = Bitwise::toBinaryString($number);
// 101

$number = "101";
$results = Bitwise::fromBinary($number);
// 5

$bit = 1;
$results = Bitwise::invertBit($bit);
// 0