CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to printf the remainder ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
johnpoo



Joined: 23 Mar 2010
Posts: 9

View user's profile Send private message

how to printf the remainder ?
PostPosted: Thu Apr 15, 2010 9:31 pm     Reply with quote

How can I printf the result of the modulus operator?
Code:

#include <16F877.h>
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,PUT

int decimal;
int b;

void main()
{
  decimal = 30;
  b = ((decimal % 2)==0);
 
  printf( "b = %u\n\r", b );
}

Thanks.
johnpoo



Joined: 23 Mar 2010
Posts: 9

View user's profile Send private message

PostPosted: Thu Apr 15, 2010 9:50 pm     Reply with quote

Actually I want to do a decimal to binary program.

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Apr 16, 2010 2:14 am     Reply with quote

b, in your example, is a number just like any other.
However, use the bit tests, a mask, or shift instructions. A lot less work...
For example:
Code:

int8 number;
int8 ctr;
int8 mask;

    mask=128;
    number=100;
    for (ctr=0;ctr<8;ctr++) {
       if (mask&number) putc('1');
       else putc('0');
       mask>>=1;
    }
   

This would output 01100100 (binary representation of 100, output MSb first).

Or:
Code:

int8 number;
int8 ctr;
int8 bit;

    number=100;
    for (ctr=0;ctr<8;ctr++) {
       bit=shift_left(&number,1,0);
       putc(bit+'0');
    }


Downside of this, is that it destroys 'number' .

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group