<< and >> ?what?
hi i dont know and never have know what do << and >> mean?
please someone explain. ive never used these 2 thingys. thankyou to anyone that helps.
i am the best
well that depends on what you use them on
in c++ you can use those with the iostream.h library and to input (>>) and output (<<) from the console with them. With fstream.h you can write to files (<<) and read from files (>>)using them.
If you use them on intergers, or shorts, or about anything but doubles (and not your own classes or structs unless you overload that operator)......this will either (<<) multiply the number by 2 or (>>) divide the number by two and is MUCH MUCH faster then a normal multiply or divide. What it actually does is shift the bits which either multiplies or divides the num by 2;
example
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
We are creating a Multi-player space strategy/shoot-em-up/RPG game.
Development is well under way and we do have a playable demo.
Always looking for help.
Digital Euphoria Soft
in c++ you can use those with the iostream.h library and to input (>>) and output (<<) from the console with them. With fstream.h you can write to files (<<) and read from files (>>)using them.
If you use them on intergers, or shorts, or about anything but doubles (and not your own classes or structs unless you overload that operator)......this will either (<<) multiply the number by 2 or (>>) divide the number by two and is MUCH MUCH faster then a normal multiply or divide. What it actually does is shift the bits which either multiplies or divides the num by 2;
example
int var = 64;var >> 1;// var now equals 32var << 1;// var now equals 64var >> 2;// var now equals 16var << 3;// var now equals 128
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
We are creating a Multi-player space strategy/shoot-em-up/RPG game.
Development is well under way and we do have a playable demo.
Always looking for help.
Digital Euphoria Soft
Depends. To the compiler, in regular C, they''re the bitwise operators. They basically shift the bits of the number left or right the number of specified positions.
Lets look at the above example. The compiler will take 56, and shift its binary position 3 to the left. 56 in binary is 111000. If we shift each digit to the left (padding the right with extra zeros as we do it), we get 111000000, which is 448. As we can see, we just multiplied 56 by 8. And 2^3 is eight. The left binary shift can be thought of as multiplying a number by 2 to the x power. Therefore, x << y is the same as x * 2^y .
Inversely, >> is the opposite of <<, and shifts the bits the the right. (For example, if we use the 56 example, and do 56 >> 3, from 111000 we go to 111, which is 7. And 56 / 8 = 7). If we have x >> y , its the same as x / 2^y .
However, when using C++ I/O functions, >> and << take on different meanings. I won''t go into too much detail, but I''m sure you''ve seen cout << "Hello"; or cin >> someInt; before.
x = 56 << 3;
Lets look at the above example. The compiler will take 56, and shift its binary position 3 to the left. 56 in binary is 111000. If we shift each digit to the left (padding the right with extra zeros as we do it), we get 111000000, which is 448. As we can see, we just multiplied 56 by 8. And 2^3 is eight. The left binary shift can be thought of as multiplying a number by 2 to the x power. Therefore, x << y is the same as x * 2^y .
Inversely, >> is the opposite of <<, and shifts the bits the the right. (For example, if we use the 56 example, and do 56 >> 3, from 111000 we go to 111, which is 7. And 56 / 8 = 7). If we have x >> y , its the same as x / 2^y .
However, when using C++ I/O functions, >> and << take on different meanings. I won''t go into too much detail, but I''m sure you''ve seen cout << "Hello"; or cin >> someInt; before.
Its called a "bit shift" operator. Its used primarily (only?) on integer variables to change their value.
For instance, just say you have an integer.
int i = 0;
In binary that would be (for the sake of argument we'll say its a byte).
00000000
if you had
int i = 1;
it would be
00000001
Now if you go
i << 1;
that means shift all the bits left once. Ie:
00000010
Which will equal 2. (if you don't understand this, go do some research on binary numbers).
If you did
i = 1;
i << 3;
it would be
00001000
Which is 8.
The >> operator does the opposite
i = 8;
i >> 3;
i is now
00000001
Getting the idea?
You can use this to save a lot of "flags" in one integer variable. You can have a whole list of #defines which you can use as flags. For instance:
#define FLAG1 1 << 0;
#define FLAG2 1 << 1;
#define FLAG3 1 << 2;
#define FLAG4 1 << 3;
Now you have an integer variable
int flags = 0;
Just say your game does something and you want to make FLAG1 true. You go:
flags |= FLAG1;
This "OR"'s the two variables together and sets it.
This will make the variable 'flags' now look like this:
00000001
To test if a flag has been set or not you use the "AND" operator
if( flags & FLAG1 ){
//FLAG1 is set to true
}
To unset a flag you use the OR operator again, but turn that bit off using the ~ operator:
flags |= ~FLAG1;
I typed this out rather quickly so a lot of it might not make sense, but hopefully it gives you a broad outline of how valuable the binary operators are (<<, >>, &, |)
Edited by - Doc Matrix on January 24, 2001 10:10:26 PM
For instance, just say you have an integer.
int i = 0;
In binary that would be (for the sake of argument we'll say its a byte).
00000000
if you had
int i = 1;
it would be
00000001
Now if you go
i << 1;
that means shift all the bits left once. Ie:
00000010
Which will equal 2. (if you don't understand this, go do some research on binary numbers).
If you did
i = 1;
i << 3;
it would be
00001000
Which is 8.
The >> operator does the opposite
i = 8;
i >> 3;
i is now
00000001
Getting the idea?
You can use this to save a lot of "flags" in one integer variable. You can have a whole list of #defines which you can use as flags. For instance:
#define FLAG1 1 << 0;
#define FLAG2 1 << 1;
#define FLAG3 1 << 2;
#define FLAG4 1 << 3;
Now you have an integer variable
int flags = 0;
Just say your game does something and you want to make FLAG1 true. You go:
flags |= FLAG1;
This "OR"'s the two variables together and sets it.
This will make the variable 'flags' now look like this:
00000001
To test if a flag has been set or not you use the "AND" operator
if( flags & FLAG1 ){
//FLAG1 is set to true
}
To unset a flag you use the OR operator again, but turn that bit off using the ~ operator:
flags |= ~FLAG1;
I typed this out rather quickly so a lot of it might not make sense, but hopefully it gives you a broad outline of how valuable the binary operators are (<<, >>, &, |)
Edited by - Doc Matrix on January 24, 2001 10:10:26 PM
----------------------------------
and is MUCH MUCH faster then a normal multiply or divide.
----------------------------------
Not really, most modern compilers will change the * and / to bitshifts to optimize the code anyway.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
The Micro$haft BSOD T-Shirt
and is MUCH MUCH faster then a normal multiply or divide.
----------------------------------
Not really, most modern compilers will change the * and / to bitshifts to optimize the code anyway.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
January 25, 2001 06:24 PM
Not really, most modern compilers will change the * and / to bitshifts to optimize the code anyway.
Not always. (y<<8)+(y<<6) is faster than (y*320), since the compiler doesn''t think to split it up like that. (last I heard, anyway)
Not always. (y<<8)+(y<<6) is faster than (y*320), since the compiler doesn''t think to split it up like that. (last I heard, anyway)
I think gcc 2.81 onwards does change a * and / to a bit shift. I remembered looking at the asm listings and that''s what I saw (that was some time ago, and I could be wrong about the compiler).
And bit shifts should only be done on unsigned integers/longs. Doing bit shifts on signed integers could be hazardous as you might change the sign bit and mess everything up.
==========================================
In a team, you either lead, follow or GET OUT OF THE WAY.
And bit shifts should only be done on unsigned integers/longs. Doing bit shifts on signed integers could be hazardous as you might change the sign bit and mess everything up.
==========================================
In a team, you either lead, follow or GET OUT OF THE WAY.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
I just checked the asm listings for code generated by lcc-win32 and MSVC Std.
Both don''t seem to change the * and / to << or >>. That sucks Does the Pro version do that?
==========================================
In a team, you either lead, follow or GET OUT OF THE WAY.
Both don''t seem to change the * and / to << or >>. That sucks Does the Pro version do that?
==========================================
In a team, you either lead, follow or GET OUT OF THE WAY.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
quote: Not always. (y<<8)+(y<<6) is faster than (y*320), since the compiler doesn''t think to split it up like that. (last I heard, anyway)
I had recently done some checking on this area and found out that with using MSVC6 on a P2-233, the * was actually faster than the << operators...
Regards,
Jumpster
Regards,JumpsterSemper Fi
Well in any case, if you program in assembler or use inline assembly in C++ bitshifting is always faster
codeXtremeif(you->intelligence < radish->intelligence) you->STFU();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement