top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C program for bit pattern search?

+5 votes
1,006 views
C program for bit pattern search?
posted Dec 30, 2013 by Giri Prasad

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
What pattern you want to check, what is the input i.e. string or 32/64 bit integer. Please complete your question.
Hi tel me for both 32 and 64 integer,string

1 Answer

0 votes

check this small program, you need to pass the pattern_mask correctly say you want to check the pattern 110 then mask would be 111 i.e. 0x07. Rest is very simple.

int pattern_check(int number, int pattern, int pattern_mask) 
{
    int count = 0;
    while(number > 0) {

        if( !((number ^ pattern) & pattern_mask) ) {
            ++count;
        }

        number >>= 1;
    }

    return count;
}

int main(void)
{

    unsigned int number = 0xa2;
    unsigned int pattern = 0x02;
    unsigned int pattern_mask = 0x03;

    int count = pattern_check(number, pattern, pattern_mask);

    printf("\ncount:  %d\n", count);

    return 0;
}
answer Dec 30, 2013 by Luv Kumar
Similar Questions
+4 votes
                                      *
                                     * *
                                    * * *
                                   * * * *
                                  * * * * * 
                                 * * * *     

I above Ouput I Missed two stars.My question is how to find out the remaining stars.Please tel me in the C programming way.

+4 votes

How to swap ith and jth Bits for a 32-Bit Integer?

+2 votes

Guys can someone help me by describing the skip-list and why someone should use it over BST.

...