• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 6544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what the hell do they do?! ive been using them, but i don''t understand what they do to the number. i know it has to do with a power of two, and one means division and the other means multiplication? given x>>3 , what would the result be, and what does the system actually do to x?

Share this post


Link to post
Share on other sites
Advertisement
Strictly speaking it''s a bit shift not a multiply divide.
>> shifts bits right. << shifts bits left.

10101010 >> 1 = 01010101
10101010 >> 2 = 00101010
10101010 >> 3 = 00010101

10101010 << 1 = 01010100
10101010 << 2 = 10101000
10101010 << 3 = 01010000

Share this post


Link to post
Share on other sites
Basically it''s a multiplication by a power of 2.
Like if x = 4 and you do x << 3 it will give you 32
Cause multiply 4, 3 times by 2.
1st time: 4 * 2 = 8
2nd time: 8 * 2 = 16
3rd time: 16 * 2 = 32
So the number tells how many time you want to multiply oy divide your number by 2.
And by the way, it''s faster than doing a simple mutiply or divide so use it when you can...

Share this post


Link to post
Share on other sites
Everything here was fine up until... "it''s faster than doing a simple mutiply or divide". Any compiler ANYWHERE will do the optimizations. It''s a bad habit to go around using shift operators. Say what you mean (ie, use * and /) and let the compiler do it quickly. Besides, you''ll end up accidentally using >> or << on non-integers, where they don''t work.

-Brian

Share this post


Link to post
Share on other sites
so, like i said, it tells the program to multiply the number times (or divinded by) two to the second number, so 7 << 3 is 7 * (2^3). thats what i said i thought it did. i was right.
all i wanted was a yes or no, but thanks for explaning anyway. anyone hear of byte shifts? i made it up; however, its plausable

Share this post


Link to post
Share on other sites
Really, you should stop thinking of it as multiplies. The bitshift operators are valid on all data types, not just integers. So it''s a mistake in logic to think << 1 on a float to mean * 2.

Share this post


Link to post
Share on other sites
i know what im doing: it multiplies or divides by powers of 2 ONLY on integers. i got it covered.
dont worry. i use it a lot anyway.
i remember it easier it i think of it as multiplies. anyways, if i need to multiply a number times 128, for example, ill know to use x<<7 .

Share this post


Link to post
Share on other sites
Just out of curiosity, why do cout and cin use the << and >> operators then?? Are they overloaded or something?

- Daniel
http://sw.mtx.net/daniel/

Share this post


Link to post
Share on other sites
Ok, if you''re still thinking of them as multiplies why use them at all? Your compiler will still turn * 128 into shift instructions when it optimizes.

iostreams using << and >> are using operator overloading. Conceptually it''s shifting information in or shifting information out. Borland''s option structures in C++ Builder use similar syntax as well. (And annoys the heck out of me in doing it.)

Share this post


Link to post
Share on other sites
The only time I really ever use bitshift operators is when I am setting, clearing, or checking bit flags. It looks something like this:

#define BITFLAG(X) (1 << X)


typedef enum{
flagnames...
}MyFlags;

MyFlags flags;

BOOL checkflag(MyFlags flagcheck)
{
return (flags & BITFLAG(flagcheck));
}

void setflag(MyFlags flagset)
{
flag = (flag / BITFLAG(flagset));
}

(that / is supposed to be a pipe (OR), the BB changed it on me)

void clearflag(MyFlags flagclear)
{
flag = (flag & (BITFLAG(flagclear)^0xFFFFFFFF));
}

So yes bitshift operators are useful, but not very often. You should only use them in situations like this when you're explicitly twiddling bits, not when you're doing math. And you really shouldn't be twiddling bits all that often either, only when cutting memory usage is an absolute priority, such as in information that travels across a network.

Later,
The Timdog

Edited by - Timdog on 3/18/00 4:26:07 AM

Edited by - Timdog on 3/18/00 4:27:18 AM

Edited by - Timdog on 3/18/00 4:27:56 AM

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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
---------------------------------------------------

Share this post


Link to post
Share on other sites

  • Advertisement