2018-05-07 12:12:58 +01:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
2018-05-07 12:12:58 +01:00
/**
* Bit shift right operation
*/
class BitShiftRight extends Operation {
/**
* BitShiftRight constructor
*/
constructor () {
super ();
this . name = "Bit shift right" ;
this . module = "Default" ;
this . description = "Shifts the bits in each byte towards the right by the specified amount.<br><br><i>Logical shifts</i> replace the leftmost bits with zeros.<br><i>Arithmetic shifts</i> preserve the most significant bit (MSB) of the original byte keeping the sign the same (positive or negative)." ;
2018-08-21 19:07:13 +01:00
this . infoURL = "https://wikipedia.org/wiki/Bitwise_operation#Bit_shifts" ;
2019-07-29 17:09:46 +01:00
this . inputType = "ArrayBuffer" ;
this . outputType = "ArrayBuffer" ;
2018-05-07 12:12:58 +01:00
this . args = [
{
"name" : "Amount" ,
"type" : "number" ,
"value" : 1
},
{
"name" : "Type" ,
"type" : "option" ,
"value" : [ "Logical shift" , "Arithmetic shift" ]
}
];
}
/**
2019-07-29 17:09:46 +01:00
* @param {ArrayBuffer} input
2018-05-07 12:12:58 +01:00
* @param {Object[]} args
2019-07-29 17:09:46 +01:00
* @returns {ArrayBuffer}
2018-05-07 12:12:58 +01:00
*/
run ( input , args ) {
const amount = args [ 0 ],
type = args [ 1 ],
mask = type === "Logical shift" ? 0 : 0x80 ;
2019-07-29 17:09:46 +01:00
input = new Uint8Array ( input );
2018-05-07 12:12:58 +01:00
return input . map ( b => {
return ( b >>> amount ) ^ ( b & mask );
2019-07-29 17:09:46 +01:00
}). buffer ;
2018-05-07 12:12:58 +01:00
}
/**
* Highlight Bit shift right
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight ( pos , args ) {
return pos ;
}
/**
* Highlight Bit shift right in reverse
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse ( pos , args ) {
return pos ;
}
}
export default BitShiftRight ;