-3

My line of programming work never really leads me into bitshift territory, which is what I think is going on here. Can anyone explain what is trying to be accomplished here? it looks like a convoluted way to accomplish a simple (number*10)

int number = 12;

std::stringstream str;

str << number << '0';

str >> number;

But why the bitshifting?

Deanie
  • 2,316
  • 2
  • 19
  • 35
KWallace
  • 1,570
  • 1
  • 15
  • 25
  • Why is Java tagged? `<<` and `>>` might have a different _meaning_ in other languages. – Sotirios Delimanolis Apr 07 '14 at 18:09
  • That's called the insertion/extraction operator I believe.. It `inserts` `number` and `'0'` into the `str` stringstream.. Then it takes `str` and `extracts` its contents into `number`. – Brandon Apr 07 '14 at 18:11
  • the << operator is not bitshifting. In C++ the << operator can be overloaded to mean different things in different contexts. You should read a primer on C++ to get more details. – Ahmed Masud Apr 07 '14 at 18:11
  • http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt – Red Alert Apr 07 '14 at 18:12
  • 2
    Why the down votes? This is an area that years ago caused a small bit of confusion for several people I knew who were well-versed in C but first learning C++. There is a much smaller pool of people like that today than there was 15 years ago, but maybe there's a similar situation for people moving to C++ from Java (which doesn't use the shift operators for I/O as far as I know). – Michael Burr Apr 07 '14 at 18:24
  • I did not downvote, but it was actually quite easy to find duplicate questions with answers by typing `[c++] "<<" ">>"` into the SO search field. – Martin R Apr 07 '14 at 18:33
  • 1
    Thanks. I'm not sure what the downvotes are for either. The initial java tag, which someone removed, is because I did not know (at the time) whether this code is C, C++, or Java. It's code someone gave me to accomplish (supposedly) a multiplication by ten. Now that I know the operators are streaming directors, I remember my "C" days, and realized right away what was going on. It's accomplishing a multiplication by ten by appending a "0" at the end. Kinda a strange way of doing it, but when someone mentioned the stream redirectors, I "got it" right away. – KWallace Apr 08 '14 at 23:04

1 Answers1

3

The standard library's IO stream interface uses the shift operators for output and input to/from streams - which is really nothing like bit shifting - by overloading those operators.

This is a situation where an operator is overloaded to do something completely different than what it normally does. That's typically something that is considered not a good practice, but this was done so long ago it's considered a "part of C++" that is more deeply ingrained than even an idiom.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • In C++, it would be more accurate to say that the built-in operators are designed to do something other than what they normally do. The most frequent and general use of `<<` and `>>` in C++ are for extraction and insertion. – James Kanze Apr 07 '14 at 18:22
  • @JamesKanze: I guess that might be one way to look at it. I'm still not a fan of using those operators for I/O, but mainly because I find it difficult to properly format things rather than because they were originally bit shifting operators. – Michael Burr Apr 07 '14 at 18:29