Thread: bit shifting

  1. #1
    Registered User cppdude's Avatar
    Join Date
    Jan 2002

    bit shifting

    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.

  2. #2
    Registered User
    Join Date
    Jan 2002
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    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.

  4. #4
    Registered User
    Join Date
    Jan 2002
    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.

  5. #5
    Registered User
    Join Date
    Mar 2002
    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.

  6. #6
    Registered User
    Join Date
    Nov 2001
    we'll call the return value ret.

    int i = ret >> (sizeof(ret)-1);

    play around with that

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Bit shifting and other bitwise operations are useful for tons of stuff, bitshifting primarily for graphics (pixel/color, etc)

  8. #8

    Ultra fast multiplication / division

    for example :

    #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;
    When you have the need for speed (in game programming)

  9. #9
    Join Date
    Aug 2001
    Groningen (NL)
    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.

  10. #10
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Quick question regardin bit shifting
    By shoobsie in forum C Programming
    Replies: 10
    Last Post: 11-04-2005, 10:46 AM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  5. bit shifting
    By Nor in forum C++ Programming
    Replies: 9
    Last Post: 08-08-2003, 11:55 AM