Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bitwise operation

Reply
Thread Tools

Bitwise operation

 
 
Magix
Guest
Posts: n/a
 
      07-30-2004
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 <<
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.



 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      07-30-2004
On Fri, 30 Jul 2004 10:11:00 +0800, "Magix" <(E-Mail Removed)> wrote in
comp.lang.c:

> 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 <<
> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-30-2004
Magix wrote on 30/07/04 :
> int MSB;
> int LSB;
> int Total;
> char buf[5];
>
> MSB = (0x07 <<
> LSB= (0xD4 & 0x00FF)
> Total = MSB | LSB
> sprintf(buf, "%d",Total);


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

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

 
Reply With Quote
 
cody
Guest
Posts: n/a
 
      07-30-2004
I should work this way but try %i instead of %d.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Magix" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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 <<
> 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.
>
>
>



 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-30-2004
cody wrote on 30/07/04 :
> I should work this way but try %i instead of %d.


Why ? What do you think the difference is with printf()?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

 
Reply With Quote
 
Tim Hagan
Guest
Posts: n/a
 
      07-30-2004
"Magix" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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 <<
> 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 << ;
LSB= (0xD4 & 0x00FF);
Total = MSB | LSB;
sprintf(buf, "%d",Total);
printf("%s\n", buf);
return 0;
}

--
Tim Hagan


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      07-30-2004
Magix wrote:
> 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 <<
> 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 << ;
LSB = (0xD4 & 0x00FF);
Total = MSB | LSB;
sprintf(buf, "%d", Total);
printf("%s\n", buf);
return 0;
}

Mine works, why not yours?
--
Joe Wright (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitwise AND operation in xslt biswaranjan.rath XML 3 11-12-2008 03:14 PM
Bitwise operation for division Jerry C Programming 7 03-02-2005 10:06 AM
Bitwise Operation Magix C Programming 5 10-15-2004 04:45 PM
Bitwise Operation Pasquale Imbemba Java 2 05-06-2004 11:19 PM
bitwise operation... Patrick Hoonhout C Programming 13 08-28-2003 08:02 PM



Advertisments