Advertisement

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

Started by March 16, 2000 10:50 PM
10 comments, last by Zipster 24 years, 7 months ago
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?
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
Advertisement
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...
---------------------------Unfortunately, no one can be told what a bug is.You have to see it for yourself...
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
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

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.
Advertisement
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 .
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/
- DanielMy homepage
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.)
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

This topic is closed to new replies.

Advertisement