Hi,
I have two char
's and I want to "stitch" them bitwise together.
For example:
char c1 = 11; // 0000 1011
char c2 = 5; // 0000 0101
short int si = stitch(c1, c2); // 0000 1011 0000 0101
So, what I first tried was with bitwise operators:
short int stitch(char c1, char c2)
{
return (c1 << 8) | c2;
}
But this doesn't work: I'm getting a short
equals to c2
... (1) Why?
(But: c1
and c2
are negative numbers in my real app... maybe this is a part of the problem?)
So, my second solution was to use a union
:
union stUnion
{
struct
{
char c1;
char c2;
}
short int si;
}
short int stitch(char c1, char c2)
{
stUnion u;
u.c1 = c1;
u.c2 = c2;
return u.si;
}
This works like I want... I think
(2) What is the best/fastest way?
Thanks!