Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(5.1):use bit mask and bit operators
  • Loading branch information
chengjieyun59 committed Feb 27, 2021
commit 479e55644ba1f2092fb2f3b7b7a0129d8f8ac80b
19 changes: 8 additions & 11 deletions chapter05/5.1 - Insertion/insertion.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
var insertion = function(N, M, i, j) {
var n = N.split('');
var m = M.split('');
var nlength = n.length - 1;
var mlength = m.length - 1;
for (var a = 0; a < j - i + 1; a++) {
console.log(m[mlength - a]);
n[nlength - (i + a)] = m[mlength - a];
}
return n.join('');
const insertion = (N, M, i, j) => {
const bitMask = (-1 << (j + 1)) | ((1 << i) - 1);
const clearedN = N & bitMask;
const shiftedM = M << i;
return clearedN | shiftedM;
};

/* TEST */
console.log(insertion('10000000000', '10011', 2, 6), '10001001100');
const N = parseInt(10000000000, 2);
const M = parseInt(10011, 2);
console.log(insertion(M, N, 2, 6).toString(2), 10001001100);