I just recently came across Shift Operators and it interests me. Though I'm beginning to learn more about it, I'm still a bit confused as where to implement them. However, the problem I'm facing is because of an example I found on MSDN

// expre_Shift_Operators.cpp
// compile with: /EHsc
// Demonstrate shift operators
#include <iostream>
using namespace std;

int main() {
   cout   << "5 times 2 is " << (5 << 1) << endl
         << "20 divided by 4 is " << (20 >> 2) << endl;
}

When I follow the instructions on a Visual Studio Command Prompt, It worked flawlessly, however, when I tried it on GCC compiler, it generated a long list of errors. It also worked correctly on GPP compiler.

Compiling on cl.exe with /EHsc:
5 times 2 is 10
20 divided by 4 is 5

Compiling on GCC:
Attached Error List

Recommended Answers

All 9 Replies

to compile c++ code, use g++ (not gcc). eg.

>g++ -Wall -std=c++98 -pedantic -Werror expre_Shift_Operators.cpp -o expre_Shift_Operators.exe

> I'm still a bit confused as where to implement them
nowhere, unless you encounter a situation where you absolutely cannot do without them.

It appears that GCC is compiling your program thinking it's a C program. If you can set DJGPP to call the compiler as "g++" instead of "gcc", it will compile and run correctly.

I'm not very conversant in DJGPP's configuration settings, nor and I clear why GCC is not correctly handling the .cpp file as it should.
Val

@vijayan121
>to compile c++ code, use g++ (not gcc)
I don't have a g++ compiler. I thought it was mostly used in linux. I'm currently just using Vista, my linux (Mandriva) is downloading process. lol

>nowhere, unless you encounter a situation
>where you absolutely cannot do without them.
Well, then it's no fun to learn new operators. You just need to make situations to learn how to implement them, and enjoy the joys of programming.

@vnames
>DJGPP to call the compiler as "g++"
>instead of "gcc"
>why GCC is not correctly handling the
>.cpp file as it should
I'm not sure how to carry that out. I also haven't done much compiling with GCC, I've been using GPP and Visual Studio C++ Express. But I though using more than one compilers will be useful for me to create portable codes.

Do you all think if it's because I'm not passing any flags like in cl.exe /EHsc, cause I've no idea about what flags it uses. I'll have to search for it.

BTW, what does /EHsc does?

> I don't have a g++ compiler
if you have gcc (the gnu compiler collection), you also have g++ (it's c++ compiler)
use it from the same command line where you earlier used gcc.

> Do you all think if it's because I'm not passing any flags like in cl.exe /EHsc?
no.

> You just need to make situations to learn how to implement them, and enjoy the joys of programming.
do not look at language facilities like a mountaineer looks at mount everest. you will not enjoy the joys of programming very much. to learn how to implement them, you could use code like what you have written

cout   << "5 times 2 is " << (5 << 1) << endl
         << "20 divided by 4 is " << (20 >> 2) << endl;

or even better,

cout   << "decimal: " << 30 << " hex: " << hex << 30 << " shifted left by 1 gives decimal: "
          dec << (30 << 1) << " hex: "  << hex << (30 << 1) << endl << dec ;

>if you have gcc (the gnu compiler collection),
>you also have g++ (it's c++ compiler)
No, I searched it. I don't have it. I'm using the DJGPP collection that I found from the website.

BTW the code you gave will generate errors.

> BTW the code you gave will generate errors.
yes, it would. modify as

cout   << "decimal: " << 30 << " hex: " << hex << 30 << " shifted left by 1 gives decimal: "
          << dec << (30 << 1) << " hex: "  << hex << (30 << 1) << endl << dec ;

> for using c++ with DJGPP
a. yes you have to use gcc on the command line (with a -X c++ switch in some cases).
b. for using c++ headers, see http://www.delorie.com/djgpp/v2faq/faq8_3.html
and you may have to use pre-standard headers <iostream.h> etc.

you really should not be using a pre-standard c++ compiler. anything released after about 2000 or so would be more or less ok.

>you may have to use pre-standard headers <iostream.h> etc.
I'm never using that header file in my entire life again. While we are at it, tell me since which year was it deprecated. I don't think that any of the new compiler has the original iostream.h in it.

> I'm never using that header file in my entire life again.
that is a wise decision.

> While we are at it, tell me since which year was it deprecated
it is not merely deprecated; it has been removed from the standard (c++98).
compilers still ship with the header though (to support legacy code).

since you are on a windows machine, you could download cygwin which has a more modern version of gcc. http://www.cygwin.com

The DJGPP is a 16-bit DOS compiler, and as such, will not likely keep up with the most recent C++ standards.

Either get Cygwin, which is a small Unix environment running under windows --it includes the most recent version of the GCC (gcc and g++ and a few other languages),

Or get MinGW, which is the GCC compiler but without the Unix environment. I personally prefer MinGW, but most people tend to like Cygwin better. You can compile both Cygwin and Windows-native applications using Cygwin. I think it is a little easier to install too...

Hope this helps.

Oh yeah, the << and >> are bitwise operators. << is shift-left and >> is shift-right.
The number 2 in binary is 0010 .
So 0010 << 2 (read the value 2 bit-shifted left two places) becomes 1000 .
Likewise, 0010 >> 1 is 0001 .
It is possible to shift too far. 0010 >> 2 is 0000 (the 1 got pushed off the end).

In C++, the << and >> operators suggested themselves to putting something in an output stream: cout << "Hello there"; and getting something from an input stream: cin >> my_int; They are still actually shift operators, but they have been overloaded to do something else. C++ doesn't prevent that. You could, for example, overload the + operator to do multiplication. It would just confuse everyone...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.