Newbie - Dont' understand << 8

  • Thread starter Thread starter Bishop
  • Start date Start date
B

Bishop

a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k +
3] << 24));

I'm trying to understand what the above line of code does. I understand
that the url[] part of (url[k + 1] << 8) returns part of the string but I
don't understand what the << 8 does.
 
Bishop said:
a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k
+ 3] << 24));

I'm trying to understand what the above line of code does. I understand
that the url[] part of (url[k + 1] << 8) returns part of the string but
I don't understand what the << 8 does.

It's a binary shift. x<<8 takes the bits of x and shifts them to the left
eight places, introducing zeroes on the right. For instance, 129<<8 would
return 33024. In binary, 129 is 10000001, and 33024 is 1000000100000000.
 
Alberto Poblacion skrev:
Bishop said:
a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) +
(url[k + 3] << 24));

I'm trying to understand what the above line of code does. I
understand that the url[] part of (url[k + 1] << 8) returns part of
the string but I don't understand what the << 8 does.

It's a binary shift. x<<8 takes the bits of x and shifts them to the
left eight places, introducing zeroes on the right. For instance, 129<<8
would return 33024. In binary, 129 is 10000001, and 33024 is
1000000100000000.

In my opinion the statement above should use binary-or (|) instead of
plus (+).

a += (UInt32)(url[k + 0] | (url[k + 1] << 8) | (url[k + 2] << 16) |
(url[k + 3] << 24));

I assume that the url[] array is unsigned bytes.
 
a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k +
3] << 24));

I'm trying to understand what the above line of code does.  I understand
that the url[] part of  (url[k + 1] << 8)  returns part of the stringbut I
don't understand what the << 8 does.

Hi,

What I do not understand is what he is doing, some kind of key
verification?
 
Ignacio said:
a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k +
3] << 24));

I'm trying to understand what the above line of code does. I understand
that the url[] part of (url[k + 1] << 8) returns part of the string but I
don't understand what the << 8 does.

What I do not understand is what he is doing, some kind of key
verification?

To me it looks more like a reimplementation of BitConverter.ToInt32 !

Arne
 
a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k +
3] << 24));
I'm trying to understand what the above line of code does.  I understand
that the url[] part of  (url[k + 1] << 8)  returns part of the string but I
don't understand what the << 8 does.

Hi,

What I do not understand is what he is doing, some kind of key
verification?

No, it's taking the 4 8-bit numbers of an ip and building the
corresponding 32 bit number.

M.
 
If you see a shift of n as a multiplication by 2^n, that is a way to write
in positional notation. As example, with base 10 instead of base 2, what is
proposed can be seen as:

firstDigit + secondDigit * 10 + thirdDigit * 100 + fourthDigit *
1000


except that here, the base is not 10, neither is it 2, but 256 (and x << 8
is like x * 256, assuming x is an integer which will not overflow when
multiplied by 256, as example).


Since the algorithm sum the result, it *may* be part of *some* check (like
someone defining his own-made CRC check), but in itself, it just 'rewrite'
four octets as one 'equivalent' 4-octets wide integer, and add the result
into a variable a.



Vanderghast, Access MVP



message
a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k
+
3] << 24));

I'm trying to understand what the above line of code does. I understand
that the url[] part of (url[k + 1] << 8) returns part of the string but I
don't understand what the << 8 does.

Hi,

What I do not understand is what he is doing, some kind of key
verification?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top