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 support@ccsinfo.com

Int8 to Binary Function Trouble

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



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

Int8 to Binary Function Trouble
PostPosted: Sat Jul 02, 2016 1:30 am     Reply with quote

Hi All

I have an issue with the the function which takes an int8 and outputs the binary equivalent:

Code:
#include <18F4620.h>

//FUSES
#FUSES NOWDT                      //No Watch Dog Timer
#FUSES INTRC                      //Internal RC Osc, with CLKOUT
#FUSES NOPROTECT                  //Code not protected from reading
#FUSES NOBROWNOUT
#FUSES NOPUT                      //No Power Up Timer
#FUSES NODEBUG                    //No Debug mode for ICD
#FUSES NOLVP                      //No low voltage programming
#FUSES NOWRT                      //Program memory not write protected
#FUSES CCP2B3                     //CCP2 output on RB3 and not RC1
#FUSES MCLR                       //Master Clear pin enabled

#define TX   PIN_C6
#define RX   PIN_C7

#use delay(clock=8000000)
#use RS232 (BAUD = 9600, XMIT = TX, RCV = RX, STREAM = COM_A, ERRORS)

//prints binary from int8 var
void printb(int8 b){
   for (int8 i = 8; i > 0; i--) {
      if(bit_test(b, i))
         printf("1");
      else
         printf("0");
   }
}

void main(void) {
   
   while(1){
      for(int8 s = 0; s < 8; s++){
         printb(s);
         printf("\n\r");
      }
   }
   
} //end main


The expected output should be binary counting to 8 however I get:

Code:
00000000
00000000                                                       
00000001                                                       
00000001                                                       
00000010                                                                     
00000010
00000011                                                       
00000011


This seems silly - the answer must be staring me in the face!

Thanks
Al
_________________
I type therefore I press buttons
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Sat Jul 02, 2016 4:08 am     Reply with quote

Bits are 7 to 0.....

You are trying to start on bit 8, and counting to bit 1.

C has a function to do this already. Look at itoa.
alyeomans



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

PostPosted: Sat Jul 02, 2016 5:34 am     Reply with quote

Thanks again, Ttelmah. I'll have a look at the itoa function but what you pointed to with the bit index range was part of the problem. It seems that the issue was in the for loop condition: testing for i>=0 in this function caused the issue.

Does not work:
Code:
void printb(int8 b){

   for (int8 i = 8; i>=0; i--) {
      if(bit_test(b, i))
         printf("1");
      else
         printf("0");
   }
}


Does work:
Code:
void printb(int8 b){

   for (int8 i = 8; i>0; i--) {
      if(bit_test(b, i-1))
         printf("1");
      else
         printf("0");
   }
}


Cheers
Al
_________________
I type therefore I press buttons
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Sat Jul 02, 2016 7:32 am     Reply with quote

With an unsigned integer, the index is _always_ going to be >=0. An unsigned can only ever be +ve, so how can it ever be <0?......

Simply use a signed int.
alyeomans



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

PostPosted: Mon Jul 04, 2016 6:54 am     Reply with quote

Thanks again...but to slide the last word in I was testing for equal or greater than 0 (i>=0) which adheres to the unsigned int rule when it failed.
A
_________________
I type therefore I press buttons
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Mon Jul 04, 2016 7:47 am     Reply with quote

The problem is that when you decrement 0, this wraps. It can never go -ve in an unsigned, so can never be <0. Think about it.
alyeomans



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

PostPosted: Tue Jul 05, 2016 12:38 am     Reply with quote

Oww yes. I see. Red face. Thank you for setting me straight
_________________
I type therefore I press buttons
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Tue Jul 05, 2016 3:32 am     Reply with quote

Don't worry.

It's one of those ones that _everyone_ here will have been caught by at some point!.... Very Happy
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