This is an attempt to modify a range of bits, and then put things back in order. I appreciated any feedback.
Code:
#include <stdio.h>
void bitpat_set ( unsigned int *valuePtr, unsigned int modValue, int start, int size)
{
//*valuePtr = 2;
int end, mask, shiftedMask;
unsigned int tempValue;
mask = 0;
end = start + size;
printf("Within the bitpat_set function\n");
mask = ( 1 << size ) -1;
printf ("Here is what is in mask %x\n", mask);
shiftedMask = mask << end;
printf("Here is what is in shiftedMask %x\n", shiftedMask);
tempValue = *valuePtr;
printf ("Here is what is in tempValue before zeroing the target region %x\n", tempValue);
tempValue = (tempValue & ~(shiftedMask));
printf ("Here is what is in tempValue after zeroing the target region %x\n", tempValue);
printf("Here is what is in value before the shift %x\n", *valuePtr);
*valuePtr = *valuePtr >> (31 - end);
printf("Here is what is in value after the shift %x\n", *valuePtr);
*valuePtr = *valuePtr & mask;
printf ("Shifting the digits back to the place they were\n");
*valuePtr = *valuePtr << ( 31 - end);
*valuePtr = *valuePtr | tempValue;
}
/**********Main Function********************/
int main ( void )
{
unsigned int *valuePtr;
unsigned int value;
value = 0xac8793bc;
valuePtr = &value;
void bitpat_set ( unsigned int *valuePtr, unsigned int modValue, int start, int size);
bitpat_set(valuePtr, 0x45, 2, 4);
printf("Within the main program\n");
printf("Here is what is in value %x\n", value);
}
Within the bitpat_set function
Here is what is in mask f
Here is what is in shiftedMask 3c0
Here is what is in tempValue before zeroing the target region ac8793bc
Here is what is in tempValue after zeroing the target region ac87903c
Here is what is in value before the shift ac8793bc
Here is what is in value after the shift 56
Shifting the digits back to the place they were
Within the main program
Here is what is in value ac87903c