Created
May 11, 2021 18:41
-
-
Save sagittaracc/e8ecdc714fdac57a1a06b0a469801666 to your computer and use it in GitHub Desktop.
Упаковка и распаковка битов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = [0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1]; | |
function sag_pack($a) { | |
$sum = 0; | |
foreach (array_reverse($a) as $index => $value) { | |
$sum += $value * (1 << $index); | |
} | |
return $sum; | |
} | |
function sag_unpack($v, $index) { | |
return ($v & (1 << $index)) > 0 ? 1 : 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment