BitShiftRight.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. /**
  8. * Bit shift right operation
  9. */
  10. class BitShiftRight extends Operation {
  11. /**
  12. * BitShiftRight constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Bit shift right";
  17. this.module = "Default";
  18. 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).";
  19. this.infoURL = "https://wikipedia.org/wiki/Bitwise_operation#Bit_shifts";
  20. this.inputType = "ArrayBuffer";
  21. this.outputType = "ArrayBuffer";
  22. this.args = [
  23. {
  24. "name": "Amount",
  25. "type": "number",
  26. "value": 1
  27. },
  28. {
  29. "name": "Type",
  30. "type": "option",
  31. "value": ["Logical shift", "Arithmetic shift"]
  32. }
  33. ];
  34. }
  35. /**
  36. * @param {ArrayBuffer} input
  37. * @param {Object[]} args
  38. * @returns {ArrayBuffer}
  39. */
  40. run(input, args) {
  41. const amount = args[0],
  42. type = args[1],
  43. mask = type === "Logical shift" ? 0 : 0x80;
  44. input = new Uint8Array(input);
  45. return input.map(b => {
  46. return (b >>> amount) ^ (b & mask);
  47. }).buffer;
  48. }
  49. /**
  50. * Highlight Bit shift right
  51. *
  52. * @param {Object[]} pos
  53. * @param {number} pos[].start
  54. * @param {number} pos[].end
  55. * @param {Object[]} args
  56. * @returns {Object[]} pos
  57. */
  58. highlight(pos, args) {
  59. return pos;
  60. }
  61. /**
  62. * Highlight Bit shift right in reverse
  63. *
  64. * @param {Object[]} pos
  65. * @param {number} pos[].start
  66. * @param {number} pos[].end
  67. * @param {Object[]} args
  68. * @returns {Object[]} pos
  69. */
  70. highlightReverse(pos, args) {
  71. return pos;
  72. }
  73. }
  74. export default BitShiftRight;