the turorial on this site does not cover bitshifting!!. I want to know how i would get the most significant bit of a return value.
please help.
the turorial on this site does not cover bitshifting!!. I want to know how i would get the most significant bit of a return value.
please help.
What I'd like to know is what bitshifting is used for besides encryption. I've studied the idea of it in C++ and Java, but no textbooks I've seen have given any practical applications of it.
In the days of old, when memory was precious and few, it is my understanding that bit packing was used to save space. Each bit could be used for holding one piece of information. Even today, I believe Windows API uses this techique to store information. In fact, the istream class has what's called a fail bit that is flipped if the user inputs data of incorrect type to be stored in variable indicated. The istream (ios?) methods fail() and good() check the failbit to determine if input succeeded or not, and clear() resets the failbit. You can do a lot of programming and never need to use bit level manipulations, but it is a rush when you get it to work.
Good point on the failbit, etc, but since there are methods that do this, why the need to learn it (other, of course, than knowledge)? Sorry if I sound dumb (it's just a fact), but why write bitshifting code?
It might be more on an assembly level than I know. But why, in a typical application, would I need to know bitshifting? I understand the idea generally in a binary way, but don't see what I'd use it for. This is my ignorance, I'm sure. Ok, you can shift values left or right, but what does that do?
Also, my question is not what was originally asked, so maybe look at cppdude's question.
You DON'T need to manipulate bits to write the average program, unless of course you are trying to impress you prof or whatever. You may never bitshift in your entire career. Knowing how to do it is nice however. Sort of like knowing the constellations, although I would never navigate by them if I had any choice.
As to the actual process....well you found me out. I don't do it either. I can probably figure it out looking up the bitwise manipulators etc. but I can't write it off the top of my head. Sort of like my foreign language classes in college. I remember the general idea, but heaven forbid that I be dropped in that country and have to survive without a reference book/translater available.
If you want to get a brief introduction to bitwise manipulations here's a reference.
http://www.stud.fim.ntnu.no/~oystesk....htm#Heading24
we'll call the return value ret.
int i = ret >> (sizeof(ret)-1);
play around with that
Bit shifting and other bitwise operations are useful for tons of stuff, bitshifting primarily for graphics (pixel/color, etc)
Also,
Ultra fast multiplication / division
for example :
When you have the need for speed (in game programming)Code:#include <iostream.h> int main(void){ int a = 10; int a1; a1 = a<<1; cout << "a = 10 = " << a << endl; cout << "a after (a<<1) = " << a1 << endl; return 0; }
In datacommunications and other more low-level C/C++ programming bitshifting is done a lot.
For example if I get two 8 bit values and want to write a 16 bit value, then I can just copy the first 8 bits to the destination block, shift the bits to left and copy the other 8 bits.
As Unregistered stated, it can also be used for fast multiplication and division. Used a lot in computing intensive applications, especially when powers of 2 are used, take a look for example at spectral analysis.
Further it is used in some encryption techniques. It can be used for rotating keys for example.
Unregistered (posted above) was me.
Bit shifting can also save space, if you saving to a file, you can have 2 or more functions working off the same number but using the different bits of that number (say, byte 1 and 2).
Last edited by ginoitalo; 03-23-2002 at 05:22 PM.