Bitwise operation

M

Magix

Hi,

I want to manipulate an incoming WORD as I receive byte by byte. let say 07
D4, which is 2004
I want to combine these two bytes and convert into decimal, then convert as
a string. (sprintf)

int MSB;
int LSB;
int Total;
char buf[5];

MSB = (0x07 << 8)
LSB= (0xD4 & 0x00FF)
Total = MSB | LSB
sprintf(buf, "%d",Total);

When I print buf in %s format, the output I expected is 2004. But it prints
"-long printf)d"
What is wrong in my code above ?

Thanks.
 
J

Jack Klein

Hi,

I want to manipulate an incoming WORD as I receive byte by byte. let say 07
D4, which is 2004
I want to combine these two bytes and convert into decimal, then convert as
a string. (sprintf)

int MSB;
int LSB;
int Total;
char buf[5];

MSB = (0x07 << 8)
LSB= (0xD4 & 0x00FF)
Total = MSB | LSB
sprintf(buf, "%d",Total);

When I print buf in %s format, the output I expected is 2004. But it prints
"-long printf)d"
What is wrong in my code above ?

Thanks.

There's nothing particularly wrong with what you posted. Copy the
real code from your editor and paste the actual text into another
post.
 
E

Emmanuel Delahaye

Magix wrote on 30/07/04 :
int MSB;
int LSB;
int Total;
char buf[5];

MSB = (0x07 << 8)
LSB= (0xD4 & 0x00FF)
Total = MSB | LSB
sprintf(buf, "%d",Total);

This code is not compiling. Please post the exact code that is not
working.
 
T

Tim Hagan

Magix said:
Hi,

I want to manipulate an incoming WORD as I receive byte by byte. let say 07
D4, which is 2004
I want to combine these two bytes and convert into decimal, then convert as
a string. (sprintf)

int MSB;
int LSB;
int Total;
char buf[5];

MSB = (0x07 << 8)
LSB= (0xD4 & 0x00FF)
Total = MSB | LSB
sprintf(buf, "%d",Total);

When I print buf in %s format, the output I expected is 2004. But it prints
"-long printf)d"
What is wrong in my code above ?

It doesn't compile. Besides, you omitted the most important piece of the
puzzle: the offending printf statement. Try this:

#include <stdio.h>

int main(void)
{
int MSB;
int LSB;
int Total;
char buf[5];

MSB = (0x07 << 8);
LSB= (0xD4 & 0x00FF);
Total = MSB | LSB;
sprintf(buf, "%d",Total);
printf("%s\n", buf);
return 0;
}
 
J

Joe Wright

Magix said:
Hi,

I want to manipulate an incoming WORD as I receive byte by byte. let say 07
D4, which is 2004
I want to combine these two bytes and convert into decimal, then convert as
a string. (sprintf)

int MSB;
int LSB;
int Total;
char buf[5];

MSB = (0x07 << 8)
LSB= (0xD4 & 0x00FF)
Total = MSB | LSB
sprintf(buf, "%d",Total);

When I print buf in %s format, the output I expected is 2004. But it prints
"-long printf)d"
What is wrong in my code above ?

Thanks.

You're missing at least three semicolons.

#include <stdio.h>
int main(void)
{
int MSB;
int LSB;
int Total;
char buf[5];
MSB = (0x07 << 8);
LSB = (0xD4 & 0x00FF);
Total = MSB | LSB;
sprintf(buf, "%d", Total);
printf("%s\n", buf);
return 0;
}

Mine works, why not yours? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top