Thread: Finding the pattern of bits inside variables

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    Finding the pattern of bits inside variables

    Greetings
    Just want to know if I can do this any differently? It seems to work. Can I improve it in any way for a specific application perhaps?

    Code:
    /* This program is supposed to extract a spcified set of bits. */
    #include <stdio.h>
    unsigned int bitpat_get (unsigned int number, int start, int end)
    {
    /*This function takes three arguments and provides the pattern between
    the values start and end given the numbering of high order to low of
    0 1 2 3 ...30 31 */
      unsigned int result, mask;
      result = 0;
      mask = 0;
      printf ("\nHere is number before shifting %x", number);
      number = number >> ( 32 - 8 );
      printf ("\nHere is the number after shifting %x", number);
      printf("\nHere is the value of mask before we shifted 1s into it %x", mask);
      mask = ( 1 << (end - start) ) - 1 ;
      printf ("\nHere is the mask after we shifted %x\n", mask);
      result = number & mask;
      return result;
    }
    /******************************Main Function***********************/
    int main (void)
    {
      unsigned int bitpat_get (unsigned int number, int start, int end);
      unsigned int w1 = 0x8abcd831;
      int x = 2, y = 8;
      printf ("Here is the output of bitpat_get() function %x\n", bitpat_get ( w1, x, y));
      return 0;
    }
    Here is the output ...

    Code:
    $ 7
    Here is number before shifting 8abcd831
    Here is the number after shifting 8a
    Here is the value of mask before we shifted 1s into it 0
    Here is the mask after we shifted 3f
    Here is the output of bitpat_get() function a

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks fine to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking inside an AAC file header for sync bits
    By compte in forum C Programming
    Replies: 3
    Last Post: 04-14-2015, 11:40 PM
  2. creating variables inside an if statement
    By nik in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2011, 03:41 PM
  3. printing with c - specify certain bits inside a byte
    By bosque in forum C Programming
    Replies: 7
    Last Post: 02-21-2011, 04:09 PM
  4. variables inside class help
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 10-02-2005, 09:15 AM
  5. Hmm.. Ai? Finding the pattern in number squences?
    By Zeusbwr in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2005, 06:13 PM

Tags for this Thread