what does << do WITH INTEGERS in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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))`

17th Jan 2024, 6:46 AM
Kuba4ful
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.
17th Jan 2024, 7:17 AM
Brian
Brian - avatar
17th Jan 2024, 7:26 AM
Bob_Li
Bob_Li - avatar
+ 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?
17th Jan 2024, 8:54 AM
Scott D
Scott D - avatar
+ 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.
17th Jan 2024, 12:33 PM
Bob_Li
Bob_Li - avatar