<< and >> (what do they do?)

Started by
10 comments, last by Zipster 24 years, 1 month ago
x <<= 3;


hehee, my CS teacher wanted to know why I was tring to stream 3 into an integer, and he was surprised that the code even compiled and worked.

heh.

Anyway, most C++ people want to totally erradicate bit shifting, because it isn''t "object oriented". In C++, << and >> are streaming operators.

aka

cout << x;

that means that you are streaming x into cout. It does make sense if you look at it.

I personally don''t like streaming (I have never used cin and cout in actual code that I program with, only in CS projects.), but you have to admit, its 1000x better than doing it C-style, where you have to tell printf() what type it is, rather than cout, which already knows how to print it.

But seeing as how i rarely ever do console apps, i have no use for cin and cout.


===============================================
"Tell brave deeds of war."
Then they recounted tales, -- "There were stern stands And bitter runs for glory."

Ah, I think there were braver deeds.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Advertisement
One thing people often forget about >> bit shift operator.
if you are shifting signed data type (int, long, short, etc...), than SAR type bit shift is used - the signum bit (most-left in binary number) is copied into new bits. i.e.
(signed chars)
-4 = 1111 1100b
-4>>1 = 1111 1110b = -2
with unsigned data types the SHR bit shift is performed:
(filling new bits with zero)
-4 = 252 = 1111 1100b
252>>1 = 011111110b = 126

usually you don''t need to care about it, because it works like it is suposed, but sometimes you may forged to use (un)signed type in right place and you may wonder, what''s going on ...


---------------------------------------------------
Ped - Peter Helcmanovsky - 7 Gods demo group
http://7gods.rulez.sk
First Sight Entertainment - http://members.xoom.com/fseteam
---------------------------------------------------
---------------------------------------------------Ped - Peter Helcmanovsky - 7 Gods demo grouphttp://7gods.rulez.skFirst Sight Entertainment - http://members.xoom.com/fseteam---------------------------------------------------

This topic is closed to new replies.

Advertisement