加入2个char到1个短 [英] Joining 2 char to 1 short
问题描述
我正在使用以下函数将2个char(字节)加入到
a 32位X86平台上的一个短片中:
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
unsigned short val = 0;
val = a;
val<< = 8;
val | = b;
返回val;
}
如果在PowerPC上编译,这也可以吗?有没有更好的办法来实现呢?
非常感谢!
史蒂夫
Hi,
I''m using the following function to join 2 char (byte) into one short on
a 32 bit X86 platform:
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
unsigned short val = 0;
val = a;
val <<= 8;
val |= b;
return val;
}
Will this also work if compiled on a PowerPC? Are there better ways to
do it?
Thanks a lot!
Steve
推荐答案
Steffen Loringer< st ************** @ freenet.de>写道:
Steffen Loringer <st**************@freenet.de> wrote:
我正在使用以下函数将2个字符(字节)加入到32位X86平台上的一个简短字中:
> unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
unsigned short val = 0;
val = a;
val<< = 8;
val | = b;
返回val;
}
如果在PowerPC上编译,这也会有效吗?
取决于你想要的。当然,_does_假设CHAR_BIT是8
而sizeof(短)至少是2.这两者都很常见;既不保证
也不保证。有可能遇到CHAR_BIT为32的设备,
和sizeof(char)== sizeof(短)== sizeof(int)== 1.
但是,由于无符号短路必须能够保持至少2 ** 16-1,
因此至少16位宽,CHAR_BIT正好是8已经
表示sizeof(short)至少为2.(暗示并不等于
其他方式;例如,sizeof(short)为2的系统,但CHAR_BIT
是9是非常合法的.36位字,char是四分之一字,短一半是
一。)
OTOH, CHAR_BIT为8的假设可以通过CHAR_BIT替换8的奇妙异乎寻常的权宜之计取消。 sizeof(short)> = 2的假设
更难摆脱。
还有更好的方法吗?
I''m using the following function to join 2 char (byte) into one short on
a 32 bit X86 platform:
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
unsigned short val = 0;
val = a;
val <<= 8;
val |= b;
return val;
}
Will this also work if compiled on a PowerPC?
Depends on what you want. Of course, it _does_ assume that CHAR_BIT is 8
and sizeof (short) is at least 2. Both of those are very common; neither
is guaranteed. It is possible to encounter devices where CHAR_BIT is 32,
and sizeof (char) == sizeof (short) == sizeof (int) == 1.
However, since an unsigned short must be able to hold at least 2**16-1,
and therefore be at least 16 bits wide, CHAR_BIT being exactly 8 already
implies sizeof (short) being at least 2. (The implication doesn''t hold
other way; for example, a system where sizeof (short) is 2, but CHAR_BIT
is 9 is quite legal. 36-bit word, char is a quarter word, short half of
one.)
OTOH, the assumption that CHAR_BIT is 8 can be removed by the
marvelously exotic expedient of replacing 8 by CHAR_BIT. The assumption
that sizeof (short) >= 2 is harder to get rid of.
Are there better ways to do it?
是的;如果你愿意在文件中注明
代码假定sizeof(短)> = 2,你的整个函数可以用
替换为
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
return(a<<< CHAR_BIT)+ b;
}
Richard
Yes; provided you are willing to put a note in the documentation that
the code assumes that sizeof (short) >= 2, your entire function can be
replaced by
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
return (a<<CHAR_BIT) + b;
}
Richard
有更好的方法吗?
是的;如果您愿意在文档中注明代码假定sizeof(短)> = 2,那么您的整个函数可以被替换为
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
返回(<<<<< CHAR_BIT)+ b;
}
Richard
Yes; provided you are willing to put a note in the documentation that
the code assumes that sizeof (short) >= 2, your entire function can be
replaced by
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
return (a<<CHAR_BIT) + b;
}
Richard
所以我假设这个函数的位移是独立的,关于
大/低端系统!?在两种情况下,编译器是否都要注意正确的转换?
So I assume bit-shifting in this function is independend concerning
big/low endian systems!? Does the compiler take care for correct
shifting in both cases?
Steffen Loringer写道:
Steffen Loringer wrote:
有更好的方法吗?
是的;如果您愿意在文档中注明代码假定sizeof(短)> = 2,那么您的整个函数可以被替换为
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
返回(<<<<< CHAR_BIT)+ b;
}
Richard
Yes; provided you are willing to put a note in the documentation that
the code assumes that sizeof (short) >= 2, your entire function can be
replaced by
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
return (a<<CHAR_BIT) + b;
}
Richard
所以我假设这个函数的位移是关于
大/低端系统的独立变换!?在这两种情况下,编译器是否都要注意正确的移位?
So I assume bit-shifting in this function is independend concerning
big/low endian systems!? Does the compiler take care for correct
shifting in both cases?
这里没有一个bigendian / littleendian问题需要担心。为什么
你认为有?
-
Chris" seeker" Dollin
生活充满了神秘感。考虑其中一个。 Sinclair,/ Babylon 5 /
There isn''t a bigendian/littleendian issue to worry about here. Why did
you think there was?
--
Chris "seeker" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
这篇关于加入2个char到1个短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!