Thread: Help - Bit Manipulation

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    Unhappy Help - Bit Manipulation

    I am trying to set a hex value to a variable and then convert that value to bits. I then need to set a value to another variable with the reverse bits.

    For example:

    x = 0x13fa

    Converted, this would equal (not accurate, but for arguments sake):

    0001 0011 1111 1001

    I need to convert this to:

    1001 1111 1100 1000

    I am having trouble with the conversion. I've been trying to use a loop like y = x<<15. y = y + x<<14, etc. but this is not working.

    Please help with some advice and/or code. Explanations would really help, as I would like to learn this.

    Thanks so much.

  2. #2
    Unregistered
    Guest
    Ignoring that a != 1001, as you stated, there has been past discussion of reversing the order of a string. Just a guess.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    This should work, but there might be better ways to do this. The PowerOf2() function should return 2^x (ie: 2^0 = 1, 2^1 =2, 2^2 = 4...). There is an existing function for this, but I can't remember its name. Pow() something.
    Code:
    const int SizeOfInt 16;
    int Variable = *whatever*;
    int TempVariable = 0;
    
    for(int i=0; i<SizeofInt; i++)
    {
       if(Variable & PowerOf2(i)) TempVariable |= PowerOf2(SizeOfInt - 1 - i);
    }
    
    Variable = TempVariable;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    *
    Guest
    Don't use an 'if'. There is no reason to test for the state of the bit. simply zero your scratch space, and mask for the '1' values and shift them into place. The zero bits will take care of themselves.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
       int x = 0xaaaa;
       int flip = 0;
       int mask = 1;
       int i, temp;
    
       printf("x:%X\n",x);
       temp = x;
       for (i=0; i<16; i++)
       {
          flip <<= 1;
          flip |= temp & mask;
          temp >>= 1;
       }
       printf("flip:%X\n",flip);

  6. #6
    Peaches
    Guest
    Darn, I had to go out so I'm too late - but what the heck:
    --------------------------------------------------------

    #include<stdio.h>
    #include<math.h>

    int main(void)
    {
    unsigned short int a, b=0, temp; /*16 bits */
    int i;
    float power; /* return type of pow() */

    a = 0x13F9; /*hex number to be reversed bit by bit */

    for (i=0; i<16; i++) /*16 bits */
    {
    power = pow(2, i); /* from 1 to 32768 */
    /* work from right to left looking at each bit */
    temp = a & (unsigned short int)power;
    if (temp > 0) /*a 1 was encountered */
    {
    /* work from left to right XORing 1's in */
    power = pow(2, 15-i);
    b = b ^ (unsigned short int)power;
    }
    }

    printf("\n%x %x", a, b);
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 09:58 AM