使用javascript模拟并行位全加法器
解析:
const A = [1, 0, 0, 1]; const B = [1, 1, 0, 1]; function plus (a, b) { let GList = [], PList = [], res = []; while(a.length) { let ai = a.pop() let bi = b.pop() let Gi = ai && bi; let Pi = ai ^ bi; let worker = new Worker('./worker.js') worker.postMessage({GList, PList}) worker.onmessage = (event) => { let ci = event.data; let si = Pi ^ ci; res.unshift(si) } GList.unshift(Gi) PList.unshift(Pi) } let worker = new Worker('./worker.js') worker.postMessage({GList, PList}) worker.onmessage = (event) => { res.unshift(event.data) console.log(res) return res; } } plus(A, B) 以上 main.js self.addEventListener('message', function (e) { let GList = e.data.GListlet PList = e.data.PListlet ci = 0; while(GList.length) { let gi = GList.pop() let pi = PList.pop() ci = gi || pi && ci; } self.postMessage(ci) self.close() }, false) 以上worker.js