3 Replies - 2271 Views - Last Post: 20 November 2007 - 05:16 PM Rate Topic: -----

#1 oXiDe  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 17-November 07

C++ Cheat Sheet wrong?

Posted 20 November 2007 - 05:04 PM

some person at my friends website posted this after seeing the C++ cheat sheet.

Quote

<< is a bit shift... left shift
>> is a bit shift... right shift
those are not IO operators...


on the cheat sheet it says

I/O Operators
>> //Input Operator
<< //Output Operator
cin >> var1, var2, var3;
cout <<"text;"<< var << endl;
cin.get(char* buffer, streamsize num, char delim ):


explain pls?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Cheat Sheet wrong?

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ Cheat Sheet wrong?

Posted 20 November 2007 - 05:08 PM

Those operators are in fact the bitshift operators, the same ones that you would see in C.

However, for certain objects (cin, cout, file streams, string streams), they have been overloaded as stream insertion and extraction operators. So for the lines:
cin >> var1, var2, var3;
cout <<"text;"<< var << endl;

they are NOT the bitshift operators - they have been overloaded to provide a different functionality.

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 cosette_hinds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-December 06

Re: C++ Cheat Sheet wrong?

Posted 20 November 2007 - 05:12 PM

View PostoXiDe, on 21 Nov, 2007 - 10:04 AM, said:

some person at my friends website posted this after seeing the C++ cheat sheet.

Quote

<< is a bit shift... left shift
>> is a bit shift... right shift
those are not IO operators...


on the cheat sheet it says

I/O Operators
>> //Input Operator
<< //Output Operator
cin >> var1, var2, var3;
cout <<"text;"<< var << endl;
cin.get(char* buffer, streamsize num, char delim ):


explain pls?


This will probably be badly worded but << is an output (insertion) operator as well as a bit shift operator. It's an example of operator overloading where the same symbol is standing for something different depending on its context. Likewise >> the extraction operator is also the right shift operator. You or that person may not have covered using these two operators if you came from a c background, but both uses try to work along with the polymorphic capabilities of C++ which enables things to stand for something different depending on context.
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: C++ Cheat Sheet wrong?

Posted 20 November 2007 - 05:16 PM

Sorry for the double post, but I thought that I'd give a quick example of both the insertion stream operator and the left bitshift operator doign their thang:
#include <iostream>

using namespace std;

int main() {
	int i=10;
	
	cout << i << endl;
	cout << (i<<2) << endl;  //left shift twice to multiply i by 4 (2*2)
	
	return 0;
}


The line cout << i << endl; does what most people would expect - it outputs the number i and a newline and flushes the buffer, so 10 is displayed.

The line after that does something a little different. (i<<2) creates a temporary variable with the same bit pattern as i, but with all of the bits shifted to the left by 2 places. The cout << portion of that line outputs this temporary variable.

Shifting the bits in a binary number to the left results in the multiplication of the original number by 2, for every bit that you bump it over. This is sometimes used as a way to efficiently get the square of the number. There are other considerations to worry about when you start using the >> and << operators as they were originally designed to be used, such as how bit-wrapping is handled, but that will hopefully suffice for now.

:)

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1