-3

For example, if I write Console.WriteLine(1<<2<<2+1); in C# console application, the output will be 32

Can you say me why? What does this "<<" operator mean? Where can I read more about it? I google but couldn't find it

David Maisuradze
  • 824
  • 2
  • 12
  • 27
  • 1
    Did you really google it? [<< Operator](https://msdn.microsoft.com/en-us/library/a1sway8w.aspx) – Izzy Jun 24 '15 at 15:39
  • and also: http://stackoverflow.com/questions/2138359/what-does-stand-for-in-c – Habib Jun 24 '15 at 15:40
  • Use http://symbolhound.com/ for searching programming terms, look at the [results](http://symbolhound.com/?q=%3C%3C+C%23), With google, usually I find it difficult to search terms related to operators in a language. – Habib Jun 24 '15 at 15:41
  • @Habib There aren't actually that many operators in C#, or most languages for that matter. You can just look up the list of operators and find the one you're interested in. – Servy Jun 24 '15 at 15:43
  • @Servy, that is true, if you know that `<<` is an operator :), look at the [result](https://www.google.ca/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=C%23+%3C%3C) of google for `C# <<` – Habib Jun 24 '15 at 15:44
  • @Habib The word "operator" is in the title of the question, so he does indeed know it's an operator. Searching on `C# << operator` (or even the exact value of the title) results in a link with a list of all of the C# operators, with `<<` being on it. – Servy Jun 24 '15 at 15:45
  • @Servy, I missed the part in title, but still, google or even SO search doesn't give results like in symbolhound. – Habib Jun 24 '15 at 15:47
  • 1
    @Dato For clarity concerning your example: because 2 + 1 is evaluated first, you get 1<<2<<3. In other words, first a bit shift to left of 2 positions, then the result is shifted with 3 positions (which has the same effect as 1<<5) – Me.Name Jun 24 '15 at 15:48
  • @Habib Sure, I'm not saying that it's wrong to use it, I'm just saying that Google works just fine as well, and saying that it's completely incapable of solving this problem is incorrect. – Servy Jun 24 '15 at 15:48
  • @Servy, I think I never said google is completely incapable, It is just hard. – Habib Jun 24 '15 at 15:49
  • Search on exact text of title, look at first result, find the section on the << operator, get answer. Doesn't seem that hard to me. It's maybe one or two seconds slower. – Servy Jun 24 '15 at 15:51
  • Ok. As it seems there is problem with my question style. I googled it like "C# <<" but got no interesting results. There first was MSDN with c# tutorials and DotNetPerls with expressions. Then my friend said me that << was operator. I didn't google any more and aksed here. Also I wanted to know why 1<<2<<2+1 equals to 32. The result was that you dislike my question and start arguing why I didn't google correctly. Sorry for this. It won't happen again. Everyone from you was beginner some times ago and I hope you know what I mean – David Maisuradze Jun 24 '15 at 15:56

1 Answers1

3

From MSDN:

The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.

https://msdn.microsoft.com/en-us/library/a1sway8w.aspx

The number 32 is returned in this case because the addition operator has precedence over the ASHL (<<) operator, but the leftmost ASHL operators are applied first. The expression is evaluated as follows:

1<<2<<2+1
((1<<2)<<(2+1))
((1<<2)<<3)
(4<<3)
32
Arin
  • 1,373
  • 2
  • 10
  • 23