# Thread: two char to one int

1. ## two char to one int

i am writing a c program using mplab and pic16f887, with the pickit2. I have a tc77, which outputs a temperature in 16 bits. I have it outputing two sets of 8 bits each, and each set will go to char a or char b. I need to take char a and char b and combine them, but not add them.

Example:
Char a: 0000 0101
Char b: 1011 0111
I need to take both char's, and put them to an int so that int c looks like this:
Int c: 0000 0101 1011 0111

I cant figure out exactly how to do this.
Any ideas?

Thanks

2. Thinking in terms of bits, you can perform a bit shift on one char and bitwise and the result with the other char.

3. Figured it out, its

c = (a << 8) + b;

4. Originally Posted by murph909
Figured it out, its

c = (a << 8) + b;
Or, quite simply ... c = (a * 256) + b;

Either way...

5. Originally Posted by murph909
Figured it out, its

c = (a << 8) + b;
Better to stay with bitwise operations i.e. logical addition instead of arithmetic.

just my 2c

6. Originally Posted by murph909
Figured it out, its

c = (a << 8) + b;
Yeah, that looks correct. By the way, I made a mistake by mentioning bitwise and instead of bitwise or, but your use of addition bypasses my mistake.

7. Why it works at all is mysterious. I would have thought shifting an 8-bit quantity left 8 positions would make it zero. Yet somewhere a conversion to int is implied. It happens BEFORE any '+' would naturally cast it to match types. Can someone shed light?

8. Originally Posted by nonoob
Why it works at all is mysterious. I would have thought shifting an 8-bit quantity left 8 positions would make it zero. Yet somewhere a conversion to int is implied. It happens BEFORE any '+' would naturally cast it to match types. Can someone shed light?
When a bit shift is performed, the integer promotions (to int or unsigned int) are performed on the operands.

9. Thanks laserlight. I must have forgotten about some of the magic that goes on behind the scenes.

10. Originally Posted by nonoob
Thanks laserlight. I must have forgotten about some of the magic that goes on behind the scenes.
As long as the lvalue is an int... it will process the rvalues as ints...
(oops, just saw that Lase beat me to it... oh well)

11. Originally Posted by CommonTater
As long as the lvalue is an int... it will process the rvalues as ints...
What exactly are you referring to when you say lvalue and rvalues? After all, in this context a and b could well be lvalues.