0
what does << do WITH INTEGERS in c++
I know what it does with streams (cout etc), but what does it do with two numbers?? I saw it in a code that went ` if (var > (600 << 16))`
4 Answers
+ 9
The << operator is a bitwise left shift operator. It shifts all bits to the left by the number of bits specified on the right-hand side of the operator.
The expression
600 << 16
shifts the number 600 leftward by 16 bits. It is the same as multiplying 600 by 2 to the 16th power, or multiplying 600 by 2 sixteen times.
When used by cout, it is still a bitwise left shift operator, but it is being overloaded by the streaming object in order to make it behave specially for streams.
+ 5
here is a nice visualization.
https://sololearn.com/compiler-playground/cuqJ9gwiUMLp/?ref=app
+ 3
Bob_Li
A quick question, with:
cout<<var1.to_string()<<'\n';
it seems to work fine as:
cout<<var1<<'\n';
Is there a reason you add "to_string()" ? Is that somewhat of a safety/compatibility measure?
+ 2
Scott D you're right. you can omit the to_string() method.
I just mindlessly copied it from the bitset conversion example I found.
It's a learning experience for me, too. Problems like these leads me to explore areas in the c++ toolset I normally leave untouched.