/*只用左右移位和一次减法的实现方法 */
#include<stdlib.h>
#include<stdio.h>
int xbyte3(unsigned word, int bytenum )
{
return ((word>>(bytenum<<3))<<24>>24)-((word>>(bytenum<<3))>>7<<31>>23);
}
int main()
{
printf("%x", xbyte3( 0xE010ffff,3) );
return 0;
}
----------------解决方案--------------------------------------------------------
为什么不考虑bytenum>3啊?
----------------解决方案--------------------------------------------------------
自己觉得方法难了点,而且用的特殊的量太多了!
----------------解决方案--------------------------------------------------------
bytenum的检测if ((~0<<2)& bytenum)
----------------解决方案--------------------------------------------------------
缺少符号扩展
int xbyte(packed_t word ,int bytenum)
{
return (((word>>(bytenum<<3))&0x80)?((word>>(bytenum<<3))&0xff|0xffffff00):((word>>(bytenum<<3))&0xff));
}
首先>>的优先级大于&的所以((word>>(bytenum<<3))&0x80)可以改为(word>>(bytenum<<3)&0x80)
第二,在扩展是先是将0xff进行转换,变为 unsiged ,unsigned 的扩展是无符号扩展!
----------------解决方案--------------------------------------------------------
bytenum的检测if ((~0<<2)& bytenum)
((~0<<2)& bytenum :bytenum 为 0、1、2、3 是返回假
[此贴子已经被作者于2007-11-5 22:46:06编辑过]
----------------解决方案--------------------------------------------------------
学习了..
----------------解决方案--------------------------------------------------------
我还需要答复!
----------------解决方案--------------------------------------------------------
首先>>的优先级大于&的所以((word>>(bytenum<<3))&0x80)可以改为(word>>(bytenum<<3)&0x80)
第二,在扩展是先是将0xff进行转换,变为 unsiged ,unsigned 的扩展是无符号扩展!
unsigned只是个幌子,是没有意义的,你用float,甚至用double* 类型都一样,你管他unsigned的扩展是不是无符号扩展干嘛
----------------解决方案--------------------------------------------------------