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... :)

Dani AI

Generated

A few points that tie the existing replies together and explain the blank output you saw.

As and said, the same symbols are used in two different contexts (bit shifts vs. stream insertion/extraction). correctly pointed out that shifting a small number right by 8/16/24 will produce zero. was right to ask you to print the values — the mystery is usually how they are printed, not whether the shifts worked.

Why you saw nothing
If the extracted values are stored as unsigned char (or printed as a char) then cout treats them as characters. A zero byte or other non-printable byte prints nothing — it looks blank. To see numeric values, either store/print them as integers or cast when printing, for example cout << static_cast<int>(byteVal).

Other important cautions

  • Use unsigned types for bit-manipulation. Right-shifting signed negatives is implementation-defined; left-shifting into the sign bit is undefined.
  • Never shift by an amount >= the width of the type (e.g., shifting a 32-bit value by 32 is undefined).
  • Prefer fixed-width types (uint32_t, etc.) from <cstdint> when dealing with bytes and masks.
  • Mask with 0xFF after shifting to isolate a byte.

Quick example to extract bytes and print them as numbers:

#include <cstdint>
#include <iostream>

uint32_t v = 0x12345678u;
unsigned b0 = (v >> 24) & 0xFFu;
unsigned b1 = (v >> 16) & 0xFFu;
unsigned b2 = (v >> 8)  & 0xFFu;
unsigned b3 = v & 0xFFu;

std::cout << b0 << ' ' << b1 << ' ' << b2 << ' ' << b3 << '\n';

Checklist when debugging this kind of code: pick a known test value (not 0..2), use unsigned/fixed-width types, print numeric casts, and enable compiler warnings.

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.