How to set MSB of a UShort Variable?
i'm having a UShort variable Temp
and which has a value 1
.
How to set the most-sign开发者_如何学JAVAificant bit of this value as 1.
You use or
to overwrite the "leftmost" bit:
ushort temp=1;
temp |= 1<<15;
Where 15
is the number of bits in your data type (16) minus 1 because your 1
is already in position 1.
Or...since you know the size of your data type:
public ushort setHighOrderBit( ushort foo )
{
return foo |= 0x8000 ;
}
[my bad. Not enough coffee this AM]
This should work:
ushort value = 307;
byte lsb = (byte)(value & 0xFFu);
byte msb = (byte)((value >> 8) & 0xFFu);
精彩评论