commit 7cf7639a2e26cb8b95151f2244fcbe1c6c6dffe6 Author: Abdussamed Date: Mon Mar 13 22:12:10 2023 +0300 First commit diff --git a/index.js b/index.js new file mode 100644 index 0000000..b0a598d --- /dev/null +++ b/index.js @@ -0,0 +1,146 @@ +/** + * Get one bit on a byte + * + * This is a byte of bits ===> ABCDEFGH + * + * getBIT(ABCDEFGH, 3) ==> E + */ +function getBIT(byte, order) +{ + return (byte >> order) & 1 +} +/** + * Update one bit on a byte + * + * This is a byte of bits ===> ABCDEFGH + * + * setBIT(ABCDEFGH, 3, 0) ==> ABCD0FGH + * setBIT(ABCDEFGH, 3, 1) ==> ABCD1FGH + */ +function setBIT(byte, order, value) +{ + return ( + value == 0 + ) ? ( + byte & (( 1 << order ) ^ 255) + ) : ( + byte | ( 1 << order ) + ); +}; +/** + * Explode byte of per bits and given Function + * and anonymous returning value is result + * + * This is a byte of bits ===> ABCDEFGH + * + * explodeBits(ABCDEFGH, (A, B, C, D, E, F, G, H) => { + * return A + * }) ===> A + * + */ +function explodeBits(bytes, map) +{ + return map( + (bytes >> 7 & 1), + (bytes >> 6 & 1), + (bytes >> 5 & 1), + (bytes >> 4 & 1), + (bytes >> 3 & 1), + (bytes >> 2 & 1), + (bytes >> 1 & 1), + bytes & 1 + ) +} +/** + * Explode byte of per bits and given Function + * and anonymous returning value is result + * + * This is a byte of bits ===> ABCDEFGH + * + * implodeByte(A,B,C,D,E,F,G,H) ===> ABCDEFGH + */ +function implodeByte(a,b,c,d,e,f,g,h) +{ + return (a << 7) | (b << 6) | (c << 5) | (d << 4) | (e << 3) | (f << 2) | (g << 1) | h +} +/** + * Explode byte of per bits and given Function + * and anonymous returning value is result + * + * This is a byte of bits ===> ABCDEFGH + * + * explodeBits(ABCDEFGH, (A0000000, 0B000000, 00C00000, 000D0000, 0000E000, 00000F00, 000000G0, 0000000H) => { + * return A + * }) ===> A + * + */ +function explodeBitValue(bytes, map) +{ + return map( + (bytes >> 7 & 1) << 7, + (bytes >> 6 & 1) << 6, + (bytes >> 5 & 1) << 5, + (bytes >> 4 & 1) << 4, + (bytes >> 3 & 1) << 3, + (bytes >> 2 & 1) << 2, + (bytes >> 1 & 1) << 1, + bytes & 1 + ) +} +/** + * Explode byte of per bits and given Function + * and anonymous returning value is result + * + * This is a byte of bits ===> ABCDEFGH + * + * implodeByte(A0000000, 0B000000, 00C00000, 000D0000, 0000E000, 00000F00, 000000G0, 0000000H) ===> ABCDEFGH + */ +function implodeBitValues(a,b,c,d,e,f,g,h) +{ + return a | b | c | d | e | f | g | h +} +/** + * This is a byte of bits ===> ABCDEFGH + * + * rightShift(ABCDEFGH) ==> HABCDEFG + */ +function rightShift(byte) +{ + return explodeBits(byte, (a,b,c,d,e,f,g,h) => { + return implodeByte(h,a,b,c,d,e,f,g) + }) +} +/** + * This is a byte of bits ===> ABCDEFGH + * + * leftShift(ABCDEFGH) ==> BCDEFGHA + */ +function leftShift(byte) +{ + return explodeBits(byte, (a,b,c,d,e,f,g,h) => { + return implodeByte(b,c,d,e,f,g,h,a) + }) +} +/** + * eachBlock([A,B,C,D,E,F,G,H], 3) ==> [A,B,C],[D,E,F],[G,H] + */ +function * eachBlock(bytesArray, size) +{ + let current = 0; + while(1) + { + let currentOffset = current * size; + let currentOffsetEnd = current * size + size; + let result = bytesArray.slice( + currentOffset, + currentOffsetEnd + ); + if(result.length + size <= size) + { + return result + }else{ + current++; + yield result + } + } +} \ No newline at end of file