Re: Joining 2 char to 1 short



Steffen Loringer <steffen.loringer@xxxxxxxxxx> writes:
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?

It will do what it says. Whether it will do what you want depends on
exactly what you want, which you haven't quite told us.

There are many possible ways to join two chars into a short. What
exactly are you trying to accomplish? Do you want the value of "a" in
the high-order bits of the result? Do you want it in the leftmost
(lowest address) portion of the result?

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: FILE objects
    ... Bill Cunningham wrote: ... Do they report to size_t? ... size of char (on x86 platform it is usually 1 byte) and the return type ...
    (comp.lang.c)
  • Re: Joining 2 char to 1 short
    ... a 32 bit X86 platform: ... unsigned short joinUnsigShort(unsigned char a,unsigned char b) ... implies sizeof being at least 2. ... marvelously exotic expedient of replacing 8 by CHAR_BIT. ...
    (comp.lang.c)
  • Re: is order urgent doubt
    ... Keith Thompson said: ... I suppose I differ from the "char ... I think that says more about the member than it does about the language. ...
    (comp.lang.c)
  • Re: about string and character
    ... Keith Thompson wrote: ... just not ones handed out on a plate. ... char a = 0; ... A single object of the char can be treated as an array of one char. ...
    (comp.lang.c)
  • Re: Joining 2 char to 1 short
    ... Steffen Loringer wrote: ... provided you are willing to put a note in the documentation that ... the code assumes that sizeof >= 2, ... unsigned short joinUnsigShort(unsigned char a,unsigned char b) ...
    (comp.lang.c)