xamples
epeat
ode
pproach
ptimize
est
A bit shift moves each digit in a set of bits left or right. The last bit in the direction of the shift is lost, and a bit is inserted on the other end.
0010 << // 0100
1011 >> // 0101
1010110 << 2 // 1011000
1011010 >> 3 // 0001011
Bit shifts can optionally take a number of times to shift
Note that each shift to the left multiplies a number by 2 to that power, and each shift to the right divides a number by two to that power (ex. a left shift of 3 multiplies the number by 8)
Write a function with the following signature:
function (binaryString, direction, numShifts) {}
function bitShift (binString, direction, numShifts) {
numShifts = numShifts || 1;
let l = binString.length,
zeroes = "0".repeat(numShifts);
if (direction === 'left') return binString.slice(numShifts, l).concat(zeroes);
else return zeroes.concat(binString.slice(0, l - numShifts));
}
REPL.IT solutions
startsWith, endsWith, includes