Shift bit when not ... bit shift operator

I need to implement a checksum (CRC16 CCITT) to check the contents of a file. The checksum is pretty simple to implement in C or Java thanks to the <and β†’ operators and the many examples available on the net.

The fact is that my checksum calculation must be implemented in VBScript.

My experience with this language is almost zero, but, in my opinion, nothing is provided for VBScript to offset bits. Therefore, I rely on multiplication and division by two. Works well, except for negative values .

I did some tests and I believe that VBScript processes its 16-bit integers with two additions.

Q1: can someone confirm this to me (two additions in VBScript)? I did not find any accurate information from the MSDN website.

Q2: Is it possible to perform a bit shift (left and right) using simple mathematical operations when a negative number is encoded with two additions?

.

Thank you very much, I would really like to avoid kludge, how to deal with integers like arrays "1" and "0" or call some java / c applications from VBScript.

EDIT thank you for your help, find below my implementation of the right shift in VBScript:

Function rightShift(value,bits) Dim res res = 65535 AND value If value>=0 Then res = res \ (2^bits) Else If value=-1 Then res = rightShift(res + 32768, bits - 1) Else res = rightShift(value \ 2 + 32768, bits - 1) End If End If rightShift = res AND 65535 End Function 

Pay attention to the code above: the value sometimes exceeded 16 bits, so I had to mask unused bits to avoid overflow ( AND 65535 ).

+6
source share
3 answers

In arithmetic with two additions, the only influence that negative values ​​have occurs when dividing by 2 by a shift to the right: the expected shift to the right will take place, but it will also introduce a new 1-bit in the most significant bit (MSB) to "keep the negative value "- if the original value was not equal to -1, in which case all the bits become 0. Therefore, to fix this, try the following pseudo-code:

 rightshift(x) { if x >= 0 return x / 2; if x < -1 return x / 2 - MINVAL; # Strip out sign bit # x must be -1, ie "all bits on" return x - MINVAL; } 

MINVAL should be a value whose representation consists only of the MSB and all other bits, which is -32768 for 16 bits. (It is named because it will be the most negative representable number using two'-complements.) Interestingly, adding MINVAL works as well as subtracting it in the specified pseudo-code, since in arithmetic with two complements x - y = x + NOT(y) + 1 , and MINVAL == NOT(MINVAL) + 1 .

Shifts to the left using multiplication by 2 work for negative numbers as well as for positive ones.

+5
source

This is not an answer, but a comment. The answer given by @j_random_hacker worked for me. But in languages ​​that do integer divisions, such as C # (assuming you can't use the built-in right shift operator for any reason), you need to round when x is odd.

 static int MINVAL = (int) -0x80000000; static int ShiftRight(int n,int bits) { //if (n >= 0) return n / (int)Math.Pow(2, bits); //double temp = n / Math.Pow(2, bits); //int r = (int) Math.Floor(temp); //return r; if (n >= 0) return n / 2; if (n < -1) return (int)Math.Round(n / (double)2, MidpointRounding.AwayFromZero) - MINVAL;//+ (n%2==0?0:-1); // Strip out sign bit // x must be -1, ie "all bits on" return n - MINVAL; } 

Now yes, C # has built-in shift operators, so this is just the purpose of learning.

0
source

It is very slow, try this. The following works for values> = 0, but will throw the array index out of bounds for bit-bits> 14 bits, and the code:

 dim ShiftArray ShiftArray = Array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,2048,4096, 8192, 16384) ' example usage dim num num = 17 num = num * ShiftArray(2) ' left shift 2 bits num = num / ShiftArray(3) ' right shift 3 bits 

Multiply the number of bits by a shift to shift left. Divide to shift to the right. This array works for 16-bit integers.

For 32-bit integers, the array will exit the array's string index for bit-bits> 30 and is equal to:

 dim ShiftArray ShiftArray = Array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824) 
-1
source

Source: https://habr.com/ru/post/914485/


All Articles