Advertisement
Pikachuun

DNH Bitwise stuff

Mar 21st, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Bitwise operations
  2. //All calculations will assume 32-bit numbers and take the required conversion steps
  3. //Original shifts/logic by Trung0246, modified by Choon
  4.  
  5. //32-bit conversions
  6. function ConvTo32Bit (a) { //Signed conversion
  7.     a = trunc(a%4294967296);
  8.     if (a > 2147483647) {
  9.         a -= 4294967296;
  10.     } else if (a < -2147483648) {
  11.         a += 4294967296;
  12.     }
  13.     return a;
  14. }
  15. function ConvTo32BitU (a) { //Unsigned conversion
  16.     a = trunc(a%4294967296);
  17.     if (a < 0) {
  18.         a += 4294967296;
  19.     }
  20.     return a;
  21. }
  22.  
  23. //Logic
  24. function NOT (a) { //~
  25.     return -(ConvTo32Bit(a) + 1);
  26. }
  27. function OR (a, b) { //|
  28.     a = ConvTo32Bit(a);
  29.     b = ConvTo32Bit(b);
  30.     let bit = 1;
  31.     let resultReturn = 0;
  32.     if (a < 0 || b < 0) {
  33.         a -= -2147483648*(a < 0); //you could use += 2147483648, but this is for stylistic purposes
  34.         b -= -2147483648*(b < 0);
  35.         resultReturn = -2147483648;
  36.     }
  37.     while (a || b) {
  38.         if (a%2 || b%2) {
  39.             resultReturn += bit;
  40.         }
  41.         a = floor(a/2);
  42.         b = floor(b/2);
  43.         bit *= 2;
  44.     }
  45.     return resultReturn;
  46. }
  47. function AND (a, b) { //&
  48.     a = ConvTo32Bit(a);
  49.     b = ConvTo32Bit(b);
  50.     let bit = 1;
  51.     let resultReturn = 0;
  52.     if (a < 0 && b < 0) {
  53.         a -= -2147483648;
  54.         b -= -2147483648;
  55.         resultReturn = -2147483648;
  56.     }
  57.     while (a || b) {
  58.         if (a%2 && b%2) {
  59.             resultReturn += bit;
  60.         }
  61.         a = floor(a/2);
  62.         b = floor(b/2);
  63.         bit *= 2;
  64.     }
  65.     return resultReturn;
  66. }
  67. function XOR (a, b) { //^
  68.     a = ConvTo32Bit(a);
  69.     b = ConvTo32Bit(b);
  70.     let bit = 1;
  71.     let resultReturn = 0;
  72.     if ((a < 0) != (b < 0)) {
  73.         a -= -2147483648*(a < 0);
  74.         b -= -2147483648*(b < 0);
  75.         resultReturn = -2147483648;
  76.     }
  77.     while (a || b) {
  78.         if (a%2 != b%2) {
  79.             resultReturn += bit;
  80.         }
  81.         a = floor(a/2);
  82.         b = floor(b/2);
  83.         bit *= 2;
  84.     }
  85.     return resultReturn;
  86. }
  87.  
  88. //Shifts
  89. function SHL (a, b) { //Bitwise Shift Left, <<
  90.     a = ConvTo32BitU(a);
  91.     b = b%32;
  92.     if (b < 0) { return SHR(a, -b); } //Shirt Right instead
  93.     ascent (i in 0..b) { a = ConvTo32BitU(a*2); } //ascent syntax will be useful for SAR
  94.     return ConvTo32Bit(a);
  95. }
  96. function SHR (a, b) { //Bitwise Logical Shift Right, >>>
  97.     a = ConvTo32BitU(a);
  98.     b = b%32;
  99.     if (b < 0) { return SHL(a, -b); } //Shift Left instead
  100.     ascent (i in 0..b) { a = ConvTo32BitU(a/2); }
  101.     return ConvTo32Bit(a);
  102. }
  103. function SAR (a, b) { //Bitwise Arithmetic Shift Right, >>
  104.     a = ConvTo32BitU(a);
  105.     b = b%32;
  106.     let c = -2147483648*(a >= 2147483648);
  107.     if (b < 0) { return SHL(a, -b); } //Shift Left instead
  108.     ascent (i in 0..b) { a = ConvTo32BitU(a/2 + c); }
  109.     return ConvTo32Bit(a);
  110. }
  111.  
  112. //Rotations (for the crazy people)
  113. function ROL (a, b) { //Bitwise Rotation Left
  114.     return OR(SHR(a, 32 - b), SHL(a, b));
  115. }
  116. function ROR (a, b) { //Bitwise Rotation Right
  117.     return OR(SHR(a, b), SHL(a, 32 - b));
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement