ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Пятница
19 июля
13742 Топик полностью
пьер (22.08.2004 15:51, просмотров: 1) ответил patton на shortup( abivan )
а ведь ссылку вроде Алексей Мусин уже давал - Bit Twiddling Hacks http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
Sign extending from a variable bit width 

Sometimes we need to extend the sign of a number but we don't know a priori
the number of bits, b, in which it is represented. (Or we could be programming
in Java, which lacks bitfields.) 

unsigned b;                 // number of bits representing the number in x
int x;                      // sign extend this b-bit number to r
int r;                      // resulting sign-extended number
const int m = 1 << (b - 1); // mask can be pre-computed if b is fixed
r = -(x & m) | x;


The code above requires five operations. Sean A. Irvine suggested that I add sign
extension methods to this page on June 13, 2004, and he provided
m = (1 << (b - 1)) - 1; r = -(x & ~m) | x; as a starting point from which I optimized.
вопрос был таков http://www.caxapa. …wwwboard.html?id=12124 ответ тогда такой:
const int m = 1 << (16 - 1);
x = low_byte | (high_byte << 8);
result = -(x & m) | x;
ну и дальше можно поиграться с "const int m" типа исключить и все такое и ни слова о short и других int'ах нет ;) как, а?