Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Full Adder algorithm (math/bits) #334

Merged
merged 3 commits into from
Apr 3, 2019

Conversation

sergiitk
Copy link
Contributor

@sergiitk sergiitk commented Apr 3, 2019

Full Adder

This method adds up two integer numbers using bitwise operators.

It implements full adder electronics circut logic to sum two 32-bit integers in two's complement format. It's using the boolean logic to cover all possible cases of adding two input bits: with and without a "carry bit" from adding the previous less-significant stage.

Legend:

  • A: Number A
  • B: Number B
  • ai: ith bit of number A
  • bi: ith bit of number B
  • carryIn: a bit carried in from the previous less-significant stage
  • carryOut: a bit to carry to the next most-significant stage
  • bitSum: The sum of ai, bi, and carryIn
  • resultBin: The full result of adding current stage with all less-significant stages (in binary)
  • resultBin: The full result of adding current stage with all less-significant stages (in decimal)
A = 3: 011
B = 6: 110
┌──────┬────┬────┬─────────┬──────────┬─────────┬───────────┬───────────┐
│  bit │ ai │ bi │ carryIn │ carryOut │  bitSum │ resultBin │ resultDec │
├──────┼────┼────┼─────────┼──────────┼─────────┼───────────┼───────────┤
│   0  │ 1  │ 0  │    0    │    0     │     1   │       1   │     1     │
│   1  │ 1  │ 1  │    0    │    1     │     0   │      01   │     1     │
│   2  │ 0  │ 1  │    1    │    1     │     0   │     001   │     1     │
│   3  │ 0  │ 0  │    1    │    0     │     1   │    1001   │     9     │
└──────┴────┴────┴─────────┴──────────┴─────────┴───────────┴───────────┘

See fullAdder.js for further details.
See Full Adder on YouTube.

@codecov
Copy link

codecov bot commented Apr 3, 2019

Codecov Report

Merging #334 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #334   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         148    149    +1     
  Lines        2600   2612   +12     
  Branches      434    434           
=====================================
+ Hits         2600   2612   +12
Impacted Files Coverage Δ
src/algorithms/math/bits/fullAdder.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 339ae02...5b7b819. Read the comment docs.

@trekhleb trekhleb merged commit 97e4f5f into trekhleb:master Apr 3, 2019
@trekhleb
Copy link
Owner

trekhleb commented Apr 3, 2019

@sergiitk, thanks for clean code and explanations! Great job!

@sergiitk sergiitk deleted the algo/math/bits/full-adder branch April 3, 2019 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants