All files / binary-operations/src binary-operations.ts

100% Statements 33/33
100% Branches 16/16
100% Functions 14/14
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 452x 2x 2x   2x 2x 2x   2x 2x 2x 2x 2x 2x 2x   2x 2x   2x 2x   2x   2x 2x   2x   2x 2x 2x   2x 2x   2x 2x   2x 4x 4x   4x 4x
export const concatBytes = (highByte: number, lowByte: number): number => {
    return bitwiseOr(shiftLeftBy8(highByte), lowByte)
}
 
export const bitwiseOr = (num1: number, num2: number): number => num1 | num2
export const bitwiseXor = (num1: number, num2: number): number => num1 ^ num2
export const bitwiseAnd = (num1: number, num2: number): number => num1 & num2
 
export const masks = {
    SECOND_DIGIT: 0x0F00,
    THIRD_DIGIT: 0x00F0,
    FOURTH_DIGIT: 0x000F,
    TWO_LAST_DIGITS: 0x00FF,
    THREE_LAST_DIGITS: 0x0FFF,
}
 
export const shiftRightBy = (offset: number) => (num: number): number => num >> offset
export const shiftLeftBy = (offset: number) => (num: number): number => num << offset
 
export const shiftRightBy8 = shiftRightBy(8)
export const shiftLeftBy8 = shiftLeftBy(8)
 
export const shiftRightBy4 = shiftRightBy(4)
 
export const shiftRightBy1 = shiftRightBy(1)
export const shiftLeftBy1 = shiftLeftBy(1)
 
export const shiftRightBy7 = shiftRightBy(7)
 
export const isolate2ndDigit = (num: number): number => shiftRightBy8(bitwiseAnd(num, masks.SECOND_DIGIT))
export const isolate3rdDigit = (num: number): number => shiftRightBy4(bitwiseAnd(num, masks.THIRD_DIGIT))
export const isolate4thDigit = (num: number): number => bitwiseAnd(num, masks.FOURTH_DIGIT)
 
export const isolate2LastDigits = (num: number): number => bitwiseAnd(num, masks.TWO_LAST_DIGITS)
export const isolate3LastDigits = (num: number): number => bitwiseAnd(num, masks.THREE_LAST_DIGITS)
 
export const isolateLeastSignificantBit = (num: number): number => bitwiseAnd(num, 0x01)
export const isolateMostSignificantBit = (num: number): number => shiftRightBy7(num)
 
export const isBitSet = (num: number, bitIndex: number): boolean => {
    const shiftRightByBitIndex = shiftRightBy(bitIndex)
    const bitMask = shiftRightByBitIndex(0x80)
 
    return bitwiseAnd(num, bitMask) !== 0;
}