-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdder.php
More file actions
31 lines (28 loc) · 835 Bytes
/
Adder.php
File metadata and controls
31 lines (28 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
require_once 'Gate.php';
/**
* Adder class, will allow two inputs (bools) to add up
*/
class Adder {
/**
* Half adder, accepts two bools returns added up
* $out0 will be true if answer is 1, false if 0 or 2
* $out1 will be true if answer is 2, false if 0 or 1
*/
static function half(bool $in0, bool $in1, &$out0, &$out1): void
{
$out0 = Gate::xor($in0, $in1);
$out1 = Gate::and($in0, $in1);
}
/**
* Full adder, accepts two bools and a carrier.
* Carrier is often $out1 reused.
* Allows adding up
*/
static function full(bool $carry, bool $in0, bool $in1, &$out0, &$out1): void
{
self::half($in0, $in1, $intern1, $intern2);
self::half($intern1, $carry, $out0, $intern3);
$out1 = Gate::or($intern2, $intern3);
}
}