# Convert decimal to binary BUT left > right

Hey, I have decimal variable that is between 0-255 and I want to convert this to a 8-bit binary but counted left to right, like this: 1 = 10000000 2 = 01000000 3 = 11000000 etc.... Is there a function for this? Or would it have to be done with a maths equation? Any advice appreciated!

I guess I could create a look up table but that's not very elegant.

You can use the bitRead and bitWrite methods to read the bits in one variable, and write them to another in reverse order.

Ah thanks, I'll give that a go. I also found this example for reversing binary: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256179312

I guess I could create a look up table but that’s not very elegant.

Why not? It’s fast, and 256 bytes of progmem is cheap.

This might work, not tested !

``````char ReverseBin(int Number) {

char temp[8] ;
int NumTemp = Number ;

for (int j = 0; j <8 ;j++) {
temp[j] = 48+ (NumTemp & 1) ;
NumTemp >> 1 ;
}
temp[8] = 0 ;

return(temp[8]) ;
}
``````

What you're looking for is a "bit order reversal" function - here's a couple of sites that might help you (or fry your brain, as the case may be!):

:)

Thanks for the advice everyone, much appreciated!