-
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;
}
-
This can't be right.
Did you even try to compile this code? If you want to shift "bit" left, you have to specify how far to shift.
This is unnecessarily complex, you don't need an extra variable. Also, you don't need to do anything if b is 0.
-
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 " be more appropriate? should i have just called "c" the variable "reverse" instead?
-
b=0 is an assignment not a comparison.