我需要內部的無符號短提取的某些位值的好方法:提取物無符號值
的USHORT內的4個報警的定義:
Name......Range.......Bits
Alarm4....0-15....13/16-16/16
Alarm3....0-15....9/16-12/16
Alarm2....0-15....5/16-8/16
Alarm1....0-15....1/16-4/16
的在USHORT的值:4383或(二進制1000100011111)
所以我想實現的是:
1001100110011001
Alarm1.....Alarm2.....Alarm3......Alarm4
1001.......1001.......1001........1001
Gets translated into:
Alarm1....Alarm2....Alarm3....Alarm4
9............9............9............9
使用的僞代碼:
getValueFunc(ushort bits, int offset);
ushort u = 4383;
UInt16 Alarm1Value = getValueFunc(u, 1);
UInt16 Alarm2Value = getValueFunc(u, 5);
UInt16 Alarm3Value = getValueFunc(u, 9);
UInt16 Alarm4Value = getValueFunc(u, 13);
問候, 約翰
非常感謝!這確實適用於所有16位都被填充的情況。經過測試,它工作,我能夠提取所有的價值。 – 2010-11-18 13:54:40