|
|
View previous topic :: View next topic |
Author |
Message |
johnpoo
Joined: 23 Mar 2010 Posts: 9
|
how to printf the remainder ? |
Posted: Thu Apr 15, 2010 9:31 pm |
|
|
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
|
|
Posted: Thu Apr 15, 2010 9:50 pm |
|
|
Actually I want to do a decimal to binary program.
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Apr 16, 2010 2:14 am |
|
|
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 |
|
|
|
|
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
|