4

Possible Duplicate:
<< and >> in C++

I don't quite understand what this means...I'm just learning C++ from my very very very basic Python experience...and so this may be a very stupid question. My question is...say you have your classic "Hello World" program and you have the line:

cout<<"Hello World!"<<endl;

what does the << mean...because I was just looking at using input in C and saw that you'd do something like:

int i;
cin>>i;

and I noticed that it has >> instead of << and I've read that those are bitwise shifts...and I don't exactly understand what those are...but I think it might be different here...Help...Thanks in advance

Community
  • 1
  • 1
JacKeown
  • 2,780
  • 7
  • 26
  • 34
  • 3
    Duplicate of http://stackoverflow.com/questions/5123674/and-in-c – J.K. Feb 25 '11 at 23:33
  • 3
    I have to admit, this blatant abuse of operator overloading convinced me that I never wanted to have anything to do with C++ ever again. – biziclop Feb 25 '11 at 23:53
  • 3
    @biziclop - why? Would you ever want to bitshift a stream? Many tokens are "overloaded" in most languages. An obvious C example would be parentheses - sometimes for precedence override, sometimes for function calls, sometimes for function prototypes, and that's just off the top of my head. Why should this particular kind of overloading be considered particularly evil? Is it really so difficult to remember that you can't bitshift a stream, but you can do stream insertions/extractions? –  Feb 26 '11 at 00:09
  • @bizclop - the main advantage of stream insertion compared with printf and family is that it's typesafe. I know many compilers can check your arguments against your format string these days - the reason that feature got added was because there were so many wrong-type errors in the past, and even now in some contexts the compile-time check cannot be done. Of course the C++ way isn't the only alternative to that, but it's among the most convenient. –  Feb 26 '11 at 00:14
  • 2
    Plus, many languages overload the `+` operator for string concatenation, etc. It really all just comes down to convention, and the `<<` operator provides a nice way to represent stream insertion. – Charles Salvia Feb 26 '11 at 00:15
  • 1
    @Steve314 That's fundamentally different. There are only a fixed number of ways parentheses work, all of them well-defined and hardcoded. Overloading an operator to do something completely unrelated would be considered a dirty hack that one should be ashamed of, not hailed as a neat solution. – biziclop Feb 26 '11 at 00:16
  • @Charles Salvia See my reply. Built-in "overloading", which isn't really overloading is fine, you learn the language once and you know what they mean for the rest of your life. However I've yet to see a single valid argument for overloading, because "you have to type less" isn't one. – biziclop Feb 26 '11 at 00:19
  • 1
    @biziclop, one argument is that operator overloading can lead to cleaner, more readable code. I mean seriously, are you going to argue that `bigint.add(50).subtract(3).divide(8)` is easier to read than `(bigint + 50 - 3) / 8`? Most languages these days offer some sort of built-in overloading anyway (e.g. `+` or `,` for string concatenation), so why not allow programmers to provide their own overloads? Yes, it can be abused or used in unintuitive ways, but so can function names. – Charles Salvia Feb 26 '11 at 00:22
  • @biziclop - there are only a fixed number of ways the << operator works in C++, at least from a high level view. Learning the language isn't so different from learning the standard library - it's all learning. I don't approve of the idea of every programmer inventing their own semantics for existing operators for every other class, but implementations of existing semantics are fine. Stream insertion and extraction are standard semantics for standard abstractions within C++. –  Feb 26 '11 at 00:24
  • @Charles Salvia It's equally easy to read for me. I can read both words and mathematical symbols. :) If you won't admit to the difference between two or three pre-defined semantics and a complete free-for-all, there's nothing I can say really. I didn't want to start a religious war anyway, it was probably a mistake to write that comment in the first place, but it's something that I remember finding extremely annoying. It's probably a matter of taste though after all. – biziclop Feb 26 '11 at 00:30
  • 2
    @biziclop, I admit there's certainly a difference between a few pre-defined operators and allowing programmers to overload symbols to mean anything. However, I think the benefit (readability) outweighs the potential for abuse. I'm always highly skeptical of the "potential for abuse" argument anyway. Really, who are these nefarious programmers who want to overload `+` to mean `-`? And if we deprive these evil bastards of operator overloading, what's to stop them from naming their multiplication functions `divide()` anyway? – Charles Salvia Feb 26 '11 at 00:36
  • 1
    @Charles Salvia The same guys who thought bitshift would be a brilliant choice for stream operations. :) – biziclop Feb 26 '11 at 00:42
  • 1
    @biziclop, touché. But what about those geniuses who thought that *much less than* would be a brilliant choice for bitshifting? – Charles Salvia Feb 26 '11 at 00:47
  • 1
    @biziclop: C++ doesn't have multimethods, but operator overloads sort of provide a statically typed analogue. That isn't possible with normal member functions. If stream insertion and extraction were operations on the stream, it wouldn't be possible to customize it for new object types. If they were operations on the object, you wouldn't be able to insert primitive types. Ok, you could use a visitor pattern, but the operators are the simplest and most flexible way to achieve a consistent syntax. – Ben Voigt Feb 26 '11 at 05:54
  • The << and >> operators **are** the I/O operators in C++. If it were not for backward compatibility with the C language, I believe the bit-shifting would have been removed from C++. Really! How often do you bit-shift stuff? – Bo Persson Feb 26 '11 at 09:50
  • @Ben: If we had `cout.print(x)` instead of `cout << x`, then you could still output new object types by providing an implicit conversion from the type to one that `print` could print. This is what Java does with its `toString` function. – dan04 Feb 26 '11 at 19:41
  • @Bo: All the time. @dan04: Creating a temporary object and copying that to the stream would be far less efficient that the current scheme. And what if the source was streaming data? You couldn't put any data into the stream until the source had generated all of it? – Ben Voigt Feb 26 '11 at 19:46
  • @Bo - I agree. I'm a fan of bit-fiddling myself, but when you fiddle with your bits, it's important that people can clearly see what you're doing. I've tried to explain this to the police on several occasions, but... well, never mind that. The point is, I for one would have no problem with spelling out "bitshl" or whatever, except for the backward compatibility thing. –  Feb 28 '11 at 06:29

2 Answers2

10

Look up C++ operator overloading. C++ allows you to overload certain operators (such as arithmetic operators like +, - or *) to provide certain functionality to user-defined classes, such as:

Foo x = 100;
Foo y = 200;
x = x + y;

The built-in C++ IOstreams library is meant to replace the C stdio.h library functions like printf. It overloads the << and >> operators to mean "insert into a stream" and "extract from a stream" respectively. So, saying:

std::cout << "Hello world";

...will insert the string "Hello World" into the standard output stream cout, which is generally associated with console output. IO Streams can be used to print something to the screen, write data to a file, insert data into a string buffer, and can be extended for many other purposes (sockets, pipes, etc.)

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
6

They're indeed bitwise shifts. Numbers in computers are represented in binary form.

Example: 10 = 1010 (8x1 + 4x0 + 2x1 + 1x0).

Now, a shift just moves all numbers to the right or left.

Left shift:
10100 and that's (16x1 + 8x0 + 4x1 + 2x0 + 1x0) or 20. You multiplied by two!

Right shift:
101 (4x1 + 2x0 + 1x0) or 5. You divided by two!

It's really just another way to divide or multiply by 2.

Now, they're all so used to pump data in a graphical way.

The data goes from your input, cin to i:

int i;
cin>>i;

And the data goes from "Hello world" to the output, cout:

cout<<"Hello World!"<<endl;
Carra
  • 17,808
  • 7
  • 62
  • 75
  • 4
    Yes, they are sometimes bitwise shifts, but with the `cin` and `cout` operators they are definitely *not*. – Andrew Feb 25 '11 at 23:40
  • I'm torn on this. I agree that these aren't bit-shifts - but it's a neat way of thinking about streams, especially if you imagine the console as being a terminal on the end of a serial cable. Obviously the RHS provides data to shift down the cable (not a shift distance) but there's still a kind of bit-shifting idea there. –  Feb 26 '11 at 00:31
  • Also, Carra did say "also used to pump data", so (like me first time through) the downvoters probably didn't read the answer properly. –  Feb 26 '11 at 00:38
  • Well, the >> && << in c++ is how I always imagine it. Very similar to console pipelines. – Carra Feb 26 '11 at 00:42