As stated, it's always backwards. Or forwards, depending on how you look at it.
If you think about it, we read decimal numbers backwards so often we don't even think about it. e.g. the number 456 is 4*100 + 5*10 + 6*1. In order to decipher normal numbers, we have to mentally take in the whole thing first before we can recon it's size. How much more efficient if the first digit was the ones place... Like, well, a computer.
Here's some code:
csharp
string ByteToBitStringNaturalOrder(byte value) {
string s = "";
for (int i = 0; i < 8; i++) {
s += (value & 1);
value >>= 1;
}
return s;
}
// ByteToBitStringNaturalOrder(5) yeilds 10100000, "backwards"
string ByteToBitString(byte value) {
string s = "";
for (int i = 0; i < 8; i++) {
s += (value & 0x80)>0 ? "1" : "0";
value <<= 1;
}
return s;
}
//ByteToBitString(5) yeilds 00000101
There are a number of ways to get it in the order you want, but with only eight bits, I like the one I used.
For completelness, here's how I'd do an array:
csharp
string ByteToBitString(byte[] values) {
string s = "";
for(int i=values.Length-1; i>=0; i--) {
s += ByteToBitString(values[i]);
}
return s;
}
I'm actually writing an arbitrary size number class in C++ right now so all the bit manip stuff is pretty fresh in the brain.
Hope this helps and that I didn't ruin the fun of doing it yourself.