Thread: modifying the bits in a specific region of an integer in C

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

    modifying the bits in a specific region of an integer in C

    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

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Step 1: make a mask that can clear the bits you want to clear.
    Code:
    // 0 is the hi bit
    mask = ((1 << size) - 1) << (CHAR_BIT * sizeof(mask) - start);
    value &= ~mask;
    Step 2: Shift the value you will set the bits into position:
    Code:
    modValue <<= (CHAR_BIT*sizeof(modValue) - start);
    Step 3: Fold:
    Code:
    value |= modValue;
    Last edited by whiteflags; 06-10-2016 at 05:19 PM.

  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. saving specific bits from a 64-bit number
    By mondaytofriday in forum C++ Programming
    Replies: 5
    Last Post: 06-25-2013, 01:45 AM
  2. Replies: 5
    Last Post: 03-16-2011, 08:40 AM
  3. Copy region of bitmap to region of rectangle
    By Sfpiano in forum Windows Programming
    Replies: 2
    Last Post: 08-18-2007, 11:58 AM
  4. How many bits are '1' in an integer variable?
    By cuthbert in forum C++ Programming
    Replies: 0
    Last Post: 09-12-2006, 01:08 PM
  5. Integer and the no. of bits it occupy
    By ramayana in forum C Programming
    Replies: 2
    Last Post: 12-15-2005, 10:06 AM

Tags for this Thread