Thread: Extracting bits

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Extracting bits

    I have a little Endian machine.
    I want to extract two bits from right side of this number 1011001

    This number is 89. If I go on moding it with 2, I'll get the desired two bits, but then how to store them in another variable ?

    Kindly suggest some other way out !

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    With the assignment operator!?

    Soma

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    If you want the right two bits you could do something like this:

    Code:
    int x = 89;
    int y = x & 0x3;
    EDIT: Or for your modulo approach:

    Code:
    int y = 89 % 2;
    Not sure if you meant the hex value or the base 10 value.
    Last edited by Syscal; 09-14-2010 at 11:50 PM.

  4. #4
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by Syscal View Post
    If you want the right two bits you could do something like this:

    Code:
    int x = 89;
    int y = x & 0x3;
    Thanks for the answer, but I forgot to mention in my first post that the variable in which I am supposed to store the extracted bits *may* contain some previously stored bits. The new bits should not overlap them !

    What to do ?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by AnishaKaul View Post
    Thanks for the answer, but I forgot to mention in my first post that the variable in which I am supposed to store the extracted bits *may* contain some previously stored bits. The new bits should not overlap them !

    What to do ?
    Then you must clear the bits in the target value to 0 first, then OR the required bits into those positions.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    o_O

    I'm not getting this. What you ask is impossible. You can't just keep on cramming more bits in the same byte.

    Can you give an example of the input you have and the output you desire?

    Soma

  7. #7
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    For that it would be:

    Code:
    int x = 89;
    y |= (x & 3);

  8. #8
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by phantomotap View Post
    Can you give an example of the input you have and the output you desire?
    Yes,
    I have an character array which is to be filled with the desired extracted bits of various numbers.

    Quote Originally Posted by Syscal View Post
    For that it would be:

    Code:
    int x = 89;
    y |= (x & 3);
    Thanks you for the information, but I did the same before you replied and this solved the problem.
    New problem was how to decide that the ANDing number has to be 3 without hard coding it, a person from LQ gave me the following formula:
    Code:
    (2^number) - 1
    where number is the number of bits to be extracted !

  9. #9
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Initially I tried to solve it by the following method: The problem with this method is that the bits get stored in the reverse order which I don't want !!

    Can some one tell me how to get around the problem in the following code !
    Code:
    unsigned int byteToBeFilled = 0;
    unsigned int holder  = 89;
    unsigned int i  = 2;
    
    message = new unsigned char [4096];
    bzero (message, 4096);
    
    while (((holder / 2) >= 1) && (i > 0))
            {
              message [byteToBeFilled] = message [byteToBeFilled] | holder % 2;
              holder = holder / 2;
    
              i--;
    
              if (i > 0)
                {
                  message [byteToBeFilled] = message [byteToBeFilled] << 1;
                }
            }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It sounds like you are building a status variable where bits are used to confirm certain actions or features. You should look into the use of named bitfields. This allows you to organize your status byte as you need and also to prevent overlaps between bits.

    Code:
    // define your bitfield
    unsigned int a : 1,
                         b : 1,
                         ...
                         y : 2,
                         z: 2;
    
    // assign your value
    y = x & 3;
    This will assign the 2 least significant bits of x into the bitfield without disturbing the other bits, which is what I believe you originally needed.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    I'm not sure what you're trying to do. If you want to extract all of the bits and move them to a new variable, just copy them. If you only want the low order bits, you'd do something like

    Code:
    unsigned int mask = (1<<length) - 1; // fill mask with <length> bits of 1s
    
    
    dest &= ~mask; // Clear out low order bits in dest
    dest |= source & mask; // copy low order bits from source to dest, leaving the rest of dest intact.
    You can shift mask up to pick something other than the least significant bits.

    If you're copying from a 32 bit value to an array of 4 8 bit values :

    Code:
    dest[0] = source;
    dest[1] = source >> 8;
    dest[2] = source >> 16;
    dest[3] = source >> 24;
    Combine the two bits of code if you only need to copy parts of source to dest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  2. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM