hello there, can anyone here explain to me and an explanation on the usage of these operators (<< and >>) or point me to any site that explains it very well... thank you... :)

Recommended Answers

All 11 Replies

Their use and meaning depends entirely on context. For built-in integral types, they are bitwise operators for bit-shifting operations. Under these circumstances, << means "shift left" and >> means "shift right".

More commonly, you see them in C++ for input and output. Under these circumstances, << means "Outputs to" and >> means "Gets input from"

the two most common examples -

#include <iostream>

int main()
{
    int i;
    std::cout << "Enter a number: ";
    std::cin >> i;
}

Line 6 of the code could be translated to read

"Enter a number: " Outputs to std::cout

Line 7 of the code could be translated to read

variable 'i' Gets input from std::cin

In both cases, the statements are read right-to-left in the code snippet.

suppose i will do this stuff

int x, y, z;
for(int i =0; i<=2; i++){
  x=(unsigned char)((i>>24) & 0xff));
  y=(unsigned char)((i>>16) & 0xff));
  z=(unsigned char)((i>>8) & 0xff));
}

what do you think will happen to the values of x, y and z?

>>what do you think will happen to the values of x, y and z?
why don't you just compile and run the program then you can find out for yourself instead of guessing.

i did, but it showed nothing.. T_T

Of course it didn't show anything because you have to add a line or two to print the value of the variables to the screen, something like this:

cout << "x = " << x << "\n";

i placed that line dude...

output:

x=
y=
z=

post complete code -- did you add the lines exactly as I posted it?

yes, i did it before you told me. here's my code sir.

#include <iostream>

using namespace std;

int main(){

int x,y,z;

for(int i =0; i<=2; i++){
   x = (unsigned char) ((i>>24) & 0xff ));
   y = (unsigned char) ((i>>16) & 0xff ));
   z = (unsigned char) ((i>>8) & 0xff ));
}
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout << "z=" << z << endl;
return 0;
}

Right shifting 1 by 8, 16 or 24 times isn't going to get you anywhere. After a single right shift, the number will become 0 and 'AND'ing it with any number would make no difference. Oh and BTW, 0XF is the same as 0XFF or 0XFFFFFFFF.

Try something like:

int main()
{
    int x,y,z, i = 16;

    x = (unsigned char) (i >> 2 & 0xff );
    y = (unsigned char) (i >> 3 & 0xF );
    z = (unsigned char) (i >> 4 & 0xFFFFFFFF );

    cout << "Original number: " << i << '\n';
    cout << "x=" << x << endl;
    cout << "y=" << y << endl;
    cout << "z=" << z << endl;
    getchar();
}

hello there, can anyone here explain to me and an explanation on the usage of these operators (<< and >>) or point me to any site that explains it very well... thank you... :)

these operators are bitwise operators....

>>is shift right operator...<< is shift left operator...

>> operator will shift as many bits as you want towards right side...

and<<operator will shift as many bits as you want towards left...
fo eg::if you shift 1 one bit toward left..now it will be10...
if we shift towards left a o will be added to right itz like mutiplying the number by 2(1X2=2 in binary itz 10)...and vice versa...

but they are commonly used for cout and cin in c++...

Thank you very much for the enlightenment.

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.