Created
September 4, 2018 12:31
-
-
Save finagin/f97cde7d111343345b22c9b9781d4d4e 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 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