Guest User

Untitled

a guest
Nov 14th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. /**
  2. *
  3. * @param {String} a
  4. * @param {String} b
  5. * @param {String} carryIn
  6. */
  7. function fullAdder(a, b, carryIn) {
  8. const [halfSum1, halfCarry1] = halfAdder(a, b);
  9. const [halfSum2, halfCarry2] = halfAdder(carryIn, halfSum1)
  10. return { sum: halfSum2, carryOut: halfCarry1 | halfCarry2 }
  11. }
  12.  
  13. /**
  14. *
  15. * @param {String} a
  16. * @param {String} b
  17. */
  18. function halfAdder(a, b) {
  19. return [a ^ b, a & b]
  20. }
  21.  
  22. /**
  23. *
  24. * @param {String} a
  25. * @param {String} b
  26. */
  27. function fullSubtractor(a, b) {
  28. return a ^ b
  29. }
  30.  
  31. /**
  32. *
  33. * @param {String} bin
  34. * @returns {Number}
  35. */
  36. function convert2Decimal(bin) {
  37. return parseInt(bin, 2)
  38. }
  39.  
  40. /**
  41. *
  42. * @param {Number} num
  43. * @param {Number} padding
  44. * @return {String}
  45. */
  46. function convert2Binary(num, padding) {
  47. return ('00000000' + (num >>> 0).toString(2)).substr(-padding);
  48. }
  49.  
  50. /**
  51. *
  52. * @param {String} num1
  53. * @param {String} num2
  54. */
  55. function subReduction(num1, num2) {
  56. let binSplit = num2.split('')
  57. let result = num1.split('').reduceRight(function performReduction(acc, curr, index) {
  58. let result = fullSubtractor(curr, binSplit[index])
  59. acc.push(result)
  60. return acc
  61. }, [])
  62. return result.reverse().join('')
  63. }
  64.  
  65. /**
  66. *
  67. * @param {String} num1
  68. * @param {String} num2
  69. */
  70. function addReduction(num1, num2) {
  71. let binSplit = num2.split('')
  72. let carryIn = 0
  73. let result = num1.split('').reduceRight(function performReduction(acc, curr, index) {
  74. const result = fullAdder(curr, binSplit[index], carryIn)
  75. carryIn = result.carryOut
  76. acc.push(result.sum)
  77. return acc
  78. }, [])
  79. carryIn === 1 ? result.push(carryIn) : null
  80. return result.reverse().join('')
  81. }
  82.  
  83. /**
  84. *
  85. * @param {String} num1
  86. * @param {String} num2
  87. * @param {Number} operationBit 0 to perform addition, 1 to perform subtraction
  88. * @returns {String}
  89. */
  90. function binaryAdder(num1, num2, operationBit = 0) {
  91. if (num1.length !== num2.length) {
  92. let l = num1.length > num2.length ? num1.length : num2.length
  93. num1 = convert2Binary(convert2Decimal(num1), l)
  94. num2 = convert2Binary(convert2Decimal(num2), l)
  95. }
  96. return operationBit ? subReduction(num1, num2) : addReduction(num1, num2)
  97. }
  98.  
  99. /**
  100. *
  101. * @param {String} num1
  102. * @param {String} num2
  103. * @returns {String}
  104. */
  105. function add(num1, num2) {
  106. const bin1 = convert2Binary(num1, 8)
  107. const bin2 = convert2Binary(num2, 8)
  108. const result = binaryAdder(bin1, bin2)
  109. return convert2Decimal(result)
  110. }
  111.  
  112. /**
  113. *
  114. * @param {String} num1 The minuend
  115. * @param {String} num2 The subtrahend
  116. */
  117. function subtract(num1, num2) {
  118. if (num1 > 255 || num2 > 255) {
  119. throw new Error('Sorry, can only handle 8-bit numbers')
  120. }
  121. const bin1 = convert2Binary(num1, 8) //minuend
  122. const bin2flip = convert2Binary(~num2, 8) // 1's complement of subtrahend
  123. const result1 = binaryAdder(bin1, bin2flip)
  124. const result2 = binaryAdder(result1, convert2Binary(1, 8))
  125. const result3 = binaryAdder(result2, convert2Binary(256, 9), 1)
  126. return convert2Decimal(result3)
  127. }
  128.  
  129. const result = subtract(200, 100)
Add Comment
Please, Sign In to add comment