我需要内部的无符号短提取的某些位值的好方法:提取物无符号值
的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