By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,420 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,420 IT Pros & Developers. It's quick & easy.

Newbie - Dont' understand << 8

P: n/a
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.
Jul 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Bishop" <in**@tgscom.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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.

Jul 7 '08 #2

P: n/a
Alberto Poblacion skrev:
"Bishop" <in**@tgscom.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>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.
>

--
Bjørn Brox
Jul 7 '08 #3

P: n/a
On Jul 7, 1:42*pm, "Bishop" <i...@tgscom.comwrote:
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?
Jul 7 '08 #4

P: n/a
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Jul 7, 1:42 pm, "Bishop" <i...@tgscom.comwrote:
>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
Jul 8 '08 #5

P: n/a
mdm
On 7 Lug, 21:26, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Jul 7, 1:42*pm, "Bishop" <i...@tgscom.comwrote:
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.
Jul 8 '08 #6

P: n/a
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

"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:0a**********************************@59g2000h sb.googlegroups.com...
On Jul 7, 1:42 pm, "Bishop" <i...@tgscom.comwrote:
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?
Jul 8 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.