Reputation: 1443
So I have the following RotateLeft algorithm in C#:
public static ushort RotateLeft(ushort value, int count)
{
int left = value << count;
int right = value >> (16 - count);
return (ushort)(left | right);
}
Does this algorithm differ if the numbering scheme is different?
By numbering scheme I mean if it's MSB-0 or LSB-0
MSB-0
0 7
1 0 0 1 0 1 1 0
LSB-0
7 0
1 0 0 1 0 1 1 0
Say I wanted to shift left by 1, does having a different numbering scheme affect the algorithm?
Upvotes: 2
Views: 123
Reputation: 21
It looks like that algorithm is agnostic of the underlying system's little- or big- endianness. That is to say, it will work the same regardless of the numbering scheme, since the OR operation adds the shifted bits back in on the other side before returning. Assuming you're using it for bit-level operations and flag checks, this might be all you need.
Without knowing how it fits into the rest of your program it's hard to say whether it will work the way you expect across different platforms using opposite bit numbering schemes. For example, if you run this on a device using MSB-0 and write some shifted data to a binary file, then read that binary data back in on a device using LSB-0, it probably won't be what you're expecting.
If your goal is to have your software work the same across differently-endian systems, take a look at the .NET BitConverter Class. If you're using this for efficient mathematical operations, the static BitConverter.IsLittleEndian field will let you check the underlying architecture so that you can shift the other way or reverse binary data accordingly.
Upvotes: 1