reverse bit with switch

This is a discussion on reverse bit with switch within the C Programming forums, part of the General Programming Boards category; What is wrong with my switch....I get the same weird value for x three times Code: #include <stdio.h> #define N ...

  1. #1
    max99
    Guest

    Exclamation reverse bit with switch

    What is wrong with my switch....I get the same weird value for x three times
    Code:
    #include <stdio.h>
    #define N (int)(8*sizeof(int))
    #define sizeofInt 32
    
    void printbits(unsigned int t);
    
    int main(void)
    {
        unsigned int x;
    	int k,i,flip,mask,flag;
    
    	flip=0;
        mask=1; 
    	
    	for (k=0;k<3;k++)
    	{
    		switch(x)
    		{
    		case 1:   x= 0xA4420810; break;
    		case 2:   x= 0xfedcba98; break;
    		case 3:   x= 0x0f0f0f0f; break;
    		}
    	
        printf("x:%X\n",x);
    	printbits(x);
    
        flag= x;
        for (i=0; i<sizeofInt; i++)
        {
           flip <<= 1;
           flip |= flag & mask;
           flag >>= 1;
        }
     	
    	printbits(flip);
    	putchar('\n');
    	}
    
    return 0;
    }
    void printbits(unsigned int t)
    {
    	int k;
    
    	for (k=0;k<N;k++,t<<=1)
    	{
    		putchar('0'+((0x80000000 &t)>>(N-1)) );
    		if ((k+1)%4==0) putchar(' ');
    	}
    	putchar('\n');
    
    	return;
    }
    Code tags added by Kermi3

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >for (k=0;k<3;k++)
    >{
    >switch(x)
    >{
    >case 1: x= 0xA4420810; break;
    >case 2: x= 0xfedcba98; break;
    >case 3: x= 0x0f0f0f0f; break;
    >}

    Just a guess, but maybe:
    Code:
    for (k=0;k<3;k++)
    {
       switch(k)
       {
          case 0: x= 0xA4420810; break;
          case 1: x= 0xfedcba98; break;
          case 2: x= 0x0f0f0f0f; break;
       }

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    Code Tags
    In the furture please use Code Tags. People will be much more likely to help you if you do. And they'll be happy about it

    Information on code tags may be found at:

    http://www.cprogramming.com/cboard/...&threadid=13473
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    thanks.........it works

  5. #5
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    I need to to put all codes to flip the bit into a reverse() function that returns an unsigned int.

    How can I do that?

    Here are my codes:

    ---------------------------------------------------------------
    #include <stdio.h>
    #define N (int)(8*sizeof(int))
    #define sizeofInt 32

    void printbits(unsigned int t);

    int main(void)
    {
    unsigned int x;
    int k,i,flip,mask,flag;

    flip=0;
    mask=1;

    for (k=0;k<3;k++)
    {
    switch(k)
    {
    case 0: x= 0xA4420810; break;
    case 1: x= 0xfedcba98; break;
    case 2: x= 0x0f0f0f0f; break;
    }

    printf("x:%X\n",x);
    printbits(x);

    flag= x;
    for (i=0; i<sizeofInt; i++)
    {
    flip <<= 1;
    flip |= flag & mask;
    flag >>= 1;
    }

    printbits(flip);
    putchar('\n');
    }

    return 0;
    }
    void printbits(unsigned int t)
    {
    int k;

    for (k=0;k<N;k++,t<<=1)
    {
    putchar('0'+((0x80000000 &t)>>(N-1)) );
    if ((k+1)%4==0) putchar(' ');
    }
    putchar('\n');

    return;
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>How can I do that?
    You can start by putting your code into code tags....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    ok ok ........I am now using code tags

    Code:
    #include <stdio.h>
    #define N (int)(8*sizeof(int))
    #define sizeofInt 32
    
    void printbits(unsigned int t);
    
    int main(void)
    {
        unsigned int x;
    	int k,i,flip,mask,flag;
    
    	flip=0;
        mask=1; 
    	
    	for (k=0;k<3;k++)
    	{
    		switch(k)
    		{
    		case 0:   x= 0xA4420810; break;
    		case 1:   x= 0xfedcba98; break;
    		case 2:   x= 0x0f0f0f0f; break;
    		}
    	
        printf("x:%X\n",x);
    	printbits(x);
    
        flag= x;
        for (i=0; i<sizeofInt; i++)
        {
           flip <<= 1;
           flip |= flag & mask;
           flag >>= 1;
        }
     	
    	printbits(flip);
    	putchar('\n');
    	}
    
    return 0;
    }
    void printbits(unsigned int t)
    {
    	int k;
    
    	for (k=0;k<N;k++,t<<=1)
    	{
    		putchar('0'+((0x80000000 &t)>>(N-1)) );
    		if ((k+1)%4==0) putchar(' ');
    	}
    	putchar('\n');
    
    	return;
    }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If all you want to do is flip the bit values, why not use xor?

    Something like this maybe:
    >printbits(x^0xffffffff);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    I don't want to use xor...because it will be too easy.....anyway I was able to blip the bits but I want to put the codes in a function called reverse().....how can do that?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So your function might look something like this :
    Code:
    void reverse(unsigned int *i)
    {
        /* Your reversing code here */
    }
    
    /* then somewhere else... */
    unsigned int myint = 0x12;
    reverse(&myint);
    Or you can do it by passing the value, and returning something at the end of the reverse function. The choice is yours.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit operation question
    By mr_coffee in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 05:00 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 06:59 AM
  4. Replies: 7
    Last Post: 12-10-2004, 07:18 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21