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