I think I got this 99.99% done, but suddenly when testing the code, it shook me that it's a mess, and I have reviewed my code over and over, can't find this tiny bug!!! I am not sure if the problem came from my 16x16 led matrix calculator or the code itself.
/*
Bit Flips Reverse
Apr. 17. 2017
Flyandance
*/
byte input[8];
byte output[8];
unsigned int in=0xAAAF, out;
void setup (){
Serial.begin(9600);
Serial.println("START:");
out=flip(in);
Serial.print("Original: ");
Serial.println(in, BIN);
Serial.print("Flipped : ");
Serial.println(out, BIN);
Serial.println("Enter Your Bits:");
}
void loop (){
while (Serial.available()>0) {
unsigned int input = Serial.parseInt();
out = flip(input);
Serial.println(input, BIN);
Serial.println(out, BIN);
}
}
/// This function reverse the direction of bits
/// ie. 0b11110000 >> 0b00001111
unsigned int flip( unsigned int v){
unsigned int r = v;
int s;
for (s=15; v; v >>= 1)
{
r <<= 1;
r |= v & 1;
s--;
}
r <<= s;
return r;
}
PaulS:
Because that is the type that the function returned.
If the function return type was shit, you wouldn't plan to eat the output, would you?
Quit arguing and change the damned return type to match what you are actually returning.
Maybe someone is too arrogant to help. My question is simple, how does an unsigned int suddenly become a signed int when returned? The unsigned int r to me is always an unsigned int, so I don't know what to change, and I highly doubt my tiny bug is from this piece of code because it's written by someone from stanford, someone actually not dumb and not helpful. Also, the fact that I have written a serial code to test that function, and it turns out running correctly, tells anyone who a brain whom is really arguing.
You can argue that the code was written by a very smart programmer until you are blue in the face. NO ONE with half a brain cell still functioning defines a function that returns an int, and then uses a return statement that returns an unsigned int. Get over it.
You have spend far more time trying to prove, without a shred of evidence, that the function works perfectly than you would have spent simply changing the return type to unsigned int, and then running the code.
Either there is no change, or the problem is solved. YOU are expected to make a modicum of effort. If you don't intend to, do not bother posting here again.
I didn't bother to reverse engineer the function flip() "written by someone from stanford, someone actually not dumb and not helpful", but tried it out.
The output certainly does not resemble what the comment would lead one to expect, regardless of whether the return type is unsigned int or signed int.
Conclusion: the person from stanford was indeed not helpful, as the OP suggested.
Unsigned int code:
/// This function reverse the direction of bits
/// ie. 0b11110000 >> 0b00001111
unsigned int flip( unsigned int v){
unsigned int r = v;
int s;
for (s=15 ; v; v >>= 1)
{
r <<= 1;
r |= v & 1;
s--;
}
r <<= s;
return r;
}
void setup() {
Serial.begin(9600);
unsigned int in=1;
unsigned int out = flip(in);
Serial.print("in: ");
Serial.println(in,BIN);
Serial.print("out: ");
Serial.print(out,BIN);
}
void loop() {}