Skip to content

Instantly share code, notes, and snippets.

@sagittaracc
Created May 11, 2021 18:41
Show Gist options
  • Save sagittaracc/e8ecdc714fdac57a1a06b0a469801666 to your computer and use it in GitHub Desktop.
Save sagittaracc/e8ecdc714fdac57a1a06b0a469801666 to your computer and use it in GitHub Desktop.
Упаковка и распаковка битов
<?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