0
What's the difference between << and >>
2 Antworten
+ 13
In general, <<, means insertion operator and also bitwise left shift operator. >>, means extraction operator and also bitwise right shift operator.
Examples
string extract_from_stream = "";
cout << "Insert something into stream!";
cin >> extract_from_stream;
int x = 5; // 0101 in binary
x = x >> 1; // 0010
cout << x ; // 2
x = x << 1; // 0100
cout << x; // 4
+ 2
'<<' is a left shift or stream output operator
'>>' — right shift or stream input
When used with streams, 'streamObject << someObject' puts 'someObject' on 'streamObject' stream; 'streamObject >> someVariable' — gets a value of someVariable's type from the streamObject stream, and puts it into someVariable.
When used with numbers, '<<' performs binary shift left operation, '>>' — shift right.