Thread: 0 1 array? bit operation?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    0 1 array? bit operation?

    (I heard of this for long... never learned it, and today it is the time...)

    I have a giant M x N matrix, and I am using a M vector and a N vector to hold 0 and 1, which indicate which column or row is active, which is updated frequently. I heard this could be done by using bit operator, but don't know how. Please give me a reference.

    Further my M or N could change too, though much less frequently. which means I need to change the indicator array or any other means accordingly as well.

    Thanks this forum!

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, implemented as you describe, bitwise operators cannot help you. They work on integer types, not arrays of 1s and 0s.

    You may be able to compact the array so that instead of storing 1s and 0s as presumably whole chars or ints, you could store them as bits that are part of ints or longs, or do similar to the collum and row vectors. Then you'd be able to use bitwise operators. If M or N are larger than 32(or possibly 64), this adds a good amount of complexity, and a fair amount if they aren't too. If the vectors are periodically read in full, this could pay off. But because of the complexity you should only do this if you have determined that accessing the vectors is a bottle neck.

    There may be libraries that do this for you.

    EDIT:
    you can also implement a bit array using bitfeilds instead of bitwise operators, but it would take more code and would be slightly slower if the index is ever determined at runtime.
    Last edited by King Mir; 12-03-2009 at 09:30 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    I thought that bit operators worked on any standard data type? So suppose we had a character you could do:

    Code:
    char a = 15;
    
    for(int i = 0; i < 4; i++)
    {
        printf("%d", (a >> i & 1));
    }
    That would print out for 1's right? I mean to ask it works with the raw bits regardless of the size. Is that not right?
    -- Will you show me how to c++?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It works on any integer type. But it will not work on an array as a whole.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #define BIT(a,n) (array)[(n)/COLS]&1<<((n)%CHAR_BIT)
    Just woke up, but that's the general idea. Yes, you can manipulate the individual bits of the elements of an array. You may actually want different arguments there, especially if you switch to two-D. But the general idea is the same.

    If you can flip the bits for a single instance of a given type, you can flip the bits of a single element of an array of any size of that same type.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    When you say "giant" what do you mean? How sparse is the array (i.e. what is the ratio of 0's to 1's)? Having an array at all might not be the best way.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  3. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  4. 128 bit uchar array? please help
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 07-31-2008, 01:41 AM
  5. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM

Tags for this Thread