Skip to content

Instantly share code, notes, and snippets.

@finagin
Created September 4, 2018 12:31
Show Gist options
  • Save finagin/f97cde7d111343345b22c9b9781d4d4e to your computer and use it in GitHub Desktop.
Save finagin/f97cde7d111343345b22c9b9781d4d4e to your computer and use it in GitHub Desktop.
Генерация таблицы истинности
<?php declare(strict_types=1);
/**
* @param int $base
*
* @return array[]
*
* @throws \InvalidArgumentException
*/
function generateTruthTable(int $base): array
{
if ($base < 0) {
throw new InvalidArgumentException('$base variable must be positive');
}
$table = [];
for ($row_num = 0, $row_count = 1 << $base; $row_num < $row_count; $row_num++) {
$row = [];
for ($cell_num = 0; $cell_num < $base; $cell_num++) {
$row[] = (bool)($row_num & 1 << $base - $cell_num - 1);
}
$table[] = $row;
}
return $table;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment