I just started coding in C++ and I saw in some example codes this symbol: <<
is there a equavalent in C# if so What is it?
Thank you in advance.

- 571
- 6
- 21
-
1It's the bit shift operator. – Luchian Grigore Sep 11 '14 at 07:11
-
2[Have a look at this](http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B) – Slyps Sep 11 '14 at 07:12
-
9@djeroen It means that now you know what its called you can use google – mathematician1975 Sep 11 '14 at 07:12
-
http://msdn.microsoft.com/en-us/library/336xbhcz.aspx – xlecoustillier Sep 11 '14 at 07:12
-
Bit shift or stream output operator – Dmitri Sosnik Sep 11 '14 at 07:12
-
2Or nested template... – Mysticial Sep 11 '14 at 07:13
-
2Like many other things, it can mean different thing depending on context. It can be the left bit-shift operator, it can be (and most probably is) the stream output operator, or it can mean something completely different depending on how it has been overloaded. – Some programmer dude Sep 11 '14 at 07:13
-
5This question appears to be off-topic because it shows zero prior research effort and the answer could be found on the internet in a few seconds. – Paul R Sep 11 '14 at 07:13
-
1One shouldn't really start *coding* in C++ without *learning* some C++ first, by following a good tutorial or a [good book (like from this list)](http://stackoverflow.com/q/388242/1782465). These would have explained it. – Angew is no longer proud of SO Sep 11 '14 at 07:13
-
C# has the same operator as well, although there it is not used for stream output, although one could write a class to provide that functionality. – PMF Sep 11 '14 at 07:13
-
Also, if you're just starting out with C++, you might want to check out [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Sep 11 '14 at 07:17
-
1@JoachimPileborg I believe the "stream output operator" is still called "bit shift operator" :) – Luchian Grigore Sep 11 '14 at 07:23
-
@Mysticial `>>` can indeed be closing a nested template, but I don't think `<<` can be opening one (you'd need an identifier betweem them). – Angew is no longer proud of SO Sep 11 '14 at 07:40
-
@Angew Ah dammit, you're right. *head against wall* – Mysticial Sep 11 '14 at 07:42
2 Answers
Disclaimer: I don't know anything about C#; this answer just describes the operator in C++.
It depends on context; that operator is often overloaded to mean different things for different types.
For integer types, it's the bitwise left shift operator; it takes the bit pattern of a value, and moves it to the left, inserting zero into the less significant bits:
unsigned x = 6; // decimal 6, binary 00110
unsigned y = x << 2; // decimal 24, binary 11000
In general, a left-shift by N bits is equivalent to multiplying by 2N (so here, shifting by 2 bits multiplies by 4).
I'm fairly sure this use of the operator is the same in C# as in C++.
The standard library overloads the operator to insert a value into an output stream, in order to produce formatted output on the console, or in files, or in other ways.
#include <iostream> // declare standard input/output streams
std::cout << 42 << std::endl; // print 42 to the console, end the line, and flush.
I think C# has a TextWriter
or something to handle formatted output, with Console.Out
or something being equivalent to std::cout
; but C# uses normal method calls rather than an overloaded operator.

- 249,747
- 28
- 448
- 644
operator<<
means exactly the same in C++ as it does in C#; it is the left-shift operator and moves all the bits in a number one bit to the left.
But, in C++, you can overload most operators to make them do whatever you like for user-defined types. Perhaps most commonly, the left- and right-shift operators are overloaded for streams to mean 'stuff this thing into that stream' (left-shift) or 'extract a variable of this type from that stream' (right-shift).

- 7,269
- 1
- 42
- 69