Thread: C prog questions... help please

  1. #16
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    how about something like this?

    I think my mind works in strange ways sometimes lol. This is to do a 4 bit word to simplify it a bit.

    Code:
     
    Unsigned int Reverse(unsigned int var1)
    {
     
                unsigned int a,b,c;
                unsigned int a = 0; // reversed number goes here
           int bit;
     
            bit = 1;             //"bit" is the variable used to mask the var1 and find the values of each bit
           c = 0;
     
    while ( bit<=8)
           {
          
               
                b = var1 & bit;   //find the value of bit 3 - XXX?   
                if (b=0)             //if the value is 1 or 0 sets the variable "a" to the flipped version
                            {
                            a = 0;    // 0000
                            c = c | a;           //this should set a rolling value of c from its initial value of 0000
                                                    //dependant on what the "bit" value is
                            }
                if (b=1)
                            {
                            a= 8;     // 1000
                            c = c | a;
                            }
     
                if (b=2)
                            {
                            a=4;      //0100
                            c = c | a;
                            }
                if (b=4)
                            {
                            a=2;      // 0010
                            c = c | a;
                            }
                if (b=8)
                            {
                            a=1;      //0001
                            c = c | a;
                            }
     
     
     
                <<bit;               //shift "bit" left to change the mask
     
                }
     
    reverse = c;
    return reverse;
     
    }

  2. #17
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    if (b=0)
    This can't be right.

    Code:
    <<bit;
    Did you even try to compile this code? If you want to shift "bit" left, you have to specify how far to shift.

    Code:
    a=0;
    c = c | a;
    This is unnecessarily complex, you don't need an extra variable. Also, you don't need to do anything if b is 0.

  3. #18
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    hi there,

    sorry i dont have a compiler on this computer and as such had to try and guess. I guess that the if(b=0) isn't required as it would automatically be a false and thus skip that. and would "
    Code:
    1<<bit;
    be more appropriate? should i have just called "c" the variable "reverse" instead?
    Last edited by willgoodenough; 04-23-2012 at 06:49 AM.

  4. #19
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    b=0 is an assignment not a comparison.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Prog
    By PreeMaria in forum C Programming
    Replies: 4
    Last Post: 11-08-2009, 10:01 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Replies: 26
    Last Post: 03-20-2004, 02:59 PM
  4. Help me name my prog.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-01-2002, 08:22 PM
  5. A few Windows prog questions
    By Garfield in forum Windows Programming
    Replies: 10
    Last Post: 11-05-2001, 07:06 PM