if i read that correctly that function will find the most significant bit that is a "1" and then set all less-significant bits to "1", regardless of what they were prior.
Which if you process that, could tell you which bit was the highest bit with a "1".
I don't see what that has to do with bitwise endian though. As if an 8bit value for the decimal number 10 was:
stored msb->lsb:
0000 1010
or lsb->msb
0101 0000
Regardless of which way it is stored, using the << and >> will have the same result because they too will be setup for that bit order.
Byte-wise endian on the other hand can be easy to check for. Tips from Fortran:
If you have an Int16 and the value is set to decimal 10, then casting it to an 8bit byte, will either give you a 0 or a 10, depending on the order.
If you can cast it to a byte[], you can do it for int32, int64 values as well.
If you really just want the index of the most significant 1s bit:
-
public static int msb(UInt16 val)
-
{
-
//16bit val
-
//Range: 0 to ((2^16)-1)
-
if ((val & 0x8000) > 0)
-
return 15;
-
if ((val & 0x4000)>0)
-
return 14;
-
if ((val & 0x2000)>0)
-
return 13;
-
if ((val & 0x1000)>0)
-
return 12;
-
-
if ((val & 0x0800)>0)
-
return 11;
-
if ((val & 0x0400)>0)
-
return 10;
-
if ((val & 0x0200)>0)
-
return 9;
-
if ((val & 0x0100)>0)
-
return 8;
-
-
if ((val & 0x0080)>0)
-
return 7;
-
if ((val & 0x0040)>0)
-
return 6;
-
if ((val & 0x0020)>0)
-
return 5;
-
if ((val & 0x0010)>0)
-
return 4;
-
if ((val & 0x0008) > 0)
-
return 3;
-
if ((val & 0x0004) > 0)
-
return 2;
-
if ((val & 0x0002) > 0)
-
return 1;
-
if ((val & 0x0001) > 0)
-
return 0;
-
return -1;
-
}
-
I have folded this into a simple for loop before, but could not find the code offhand and didn't want to think about it counting down (i think the magic number is like 17 or something?)