454,420 Members | 1,430 Online
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
6 Replies

 P: n/a "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. 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" 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. Iunderstand that the url[] part of (url[k + 1] << 8) returns part ofthe 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"

 P: n/a Ignacio Machin ( .NET/ C# MVP ) wrote: On Jul 7, 1:42 pm, "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 understandthat the url[] part of (url[k + 1] << 8) returns part of the string but Idon'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 On 7 Lug, 21:26, "Ignacio Machin ( .NET/ C# MVP )"

 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 )"