The << symbol.

M

markengley

Hi everyone hope you are all ok.

Just learning C++ (as you can tell from the noob question Im about to
ask). One symbol/function/assignment/whatever really bothers me which
is the "<<" one.

I saw this line of code:

#define IN_VALIDINPUT (1 << 16)

And its meaning totally stumps me. I know that << gets used for like:

cout << "My name isnt Ishmael";

and that these can stack up:

cout << "My name isnt Ishmael, Its " << name << "You fool.";

but I dont understand if these 2 contexts for << are even the same. I
also know that there is a similar thing just the opposite facing (>>)
but again the use stumps me.

Sorry if this is a silly line of questioning but searching for
anything on the topic of "<<" throws up endless tutorial programs full
of:
cout << "Hello World^42";

could really do with some guidance.
 
V

Victor Bazarov

Just learning C++ (as you can tell from the noob question Im about to
ask). One symbol/function/assignment/whatever really bothers me which
is the "<<" one.

I saw this line of code:

#define IN_VALIDINPUT (1 << 16)

It's an operator meaning "left shift" for integral values.
And its meaning totally stumps me. I know that << gets used for like:

cout << "My name isnt Ishmael";

That operator is overloadable and it has a different meaning for
the 'cout' object.
and that these can stack up:

cout << "My name isnt Ishmael, Its " << name << "You fool.";

but I dont understand if these 2 contexts for << are even the same. I
also know that there is a similar thing just the opposite facing (>>)
but again the use stumps me.

Yes, for integers the >> means "right shift". And it's also overloaded
for streams and it means something different there.
Sorry if this is a silly line of questioning but searching for
anything on the topic of "<<" throws up endless tutorial programs full
of:
cout << "Hello World^42";

could really do with some guidance.

You should allow those symbols to mean different things for now and
you will learn later _how_ it's possible that they mean different
things for different objects.

V
 
M

markengley

"> It's an operator meaning "left shift" for integral values."

integral values? do you mean integers?

Also do you mean a bitwise left shift. so 2 becomes 4 when shifted
once, 64 becomes 256 when shifted twice? Am I thinking back to my
Verilog days too much here?
 
A

Alan Johnson

"> It's an operator meaning "left shift" for integral values."

integral values? do you mean integers?

Essentially. C++ has a number of integral types (types that can hold
integers), that vary in the range they can express (and, usually, in the
number of bytes used to represent them).
Also do you mean a bitwise left shift. so 2 becomes 4 when shifted
once, 64 becomes 256 when shifted twice?

Yes.
 
M

markengley

Essentially. C++ has a number of integral types (types that can hold
integers), that vary in the range they can express (and, usually, in the
number of bytes used to represent them).




Yes.

Thats awesome thanks people.
 
M

markengley

Essentially. C++ has a number of integral types (types that can hold
integers), that vary in the range they can express (and, usually, in the
number of bytes used to represent them).




Yes.

Thats awesome thanks people.
 
D

dervaish

Hi everyone hope you are all ok.

Just learning C++ (as you can tell from the noob question Im about to
ask). One symbol/function/assignment/whatever really bothers me which
is the "<<" one.

I saw this line of code:

#define IN_VALIDINPUT (1 << 16)

And its meaning totally stumps me. I know that << gets used for like:

cout << "My name isnt Ishmael";

and that these can stack up:

cout << "My name isnt Ishmael, Its " << name << "You fool.";

but I dont understand if these 2 contexts for << are even the same. I
also know that there is a similar thing just the opposite facing (>>)
but again the use stumps me.

Sorry if this is a silly line of questioning but searching for
anything on the topic of "<<" throws up endless tutorial programs full
of:
cout << "Hello World^42";

could really do with some guidance.

The '<<' in C has a different meaning (left shift bitwise operator)
that is if i do 2<<1, the result would be 4. But because of operator
overloading concept in C++, that is we can have different meaning of
the operator, the "<<" operator is overloaded in C++ for standard
input/output streams hence when you have cout<<"Hello World";, its the
overloaded operator that would be invoked as cout is the object here.
But if you have same operator with integer values on both sides, it
would be a general bitwise shift left. For further reference see
Design and Evolution of C++ by Bjarne Stroustrup
 
M

markengley

The '<<' in C has a different meaning (left shift bitwise operator)
that is if i do 2<<1, the result would be 4. But because of operator
overloading concept in C++, that is we can have different meaning of
the operator, the "<<" operator is overloaded in C++ for standard
input/output streams hence when you have cout<<"Hello World";, its the
overloaded operator that would be invoked as cout is the object here.
But if you have same operator with integer values on both sides, it
would be a general bitwise shift left. For further reference see
Design and Evolution of C++ by Bjarne Stroustrup

Hmm thanks, Id heard people say that operator was overloaded but that
makes sense of it. Also it occured to me to ask if there is any way
to check the carry for this operation, or for any other bitwise
operation for that matter?
 
R

red floyd

Hmm thanks, Id heard people say that operator was overloaded but that
makes sense of it. Also it occured to me to ask if there is any way
to check the carry for this operation, or for any other bitwise
operation for that matter?

Nope, because shifting left and creating a value bigger than the
underlying integral type can represent is undefined behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top