我需要一种从 unsigned short 中的某些位中提取值的好方法:
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);
问候,约翰