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

Bit_test function and pointers

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



Joined: 20 Jul 2004
Posts: 34

View user's profile Send private message Send e-mail

Bit_test function and pointers
PostPosted: Tue Sep 20, 2005 1:46 am     Reply with quote

Hi,

I have a problem using the bit_test function with pointers.
bit_clear and bit_set will both work perfectly, but the following:

Code:

int8 * myPtr;

myPtr = &(MyStruct[2].MyElement[6]);

if(Alpha)
{
   bit_set(*myPtr,0); //OK
}
else
{
 bit_clear(*myPtr,0); //OK
}

if(bit_test(*myPtr,0)) //NOT OK. Will return TRUE always!!!
{

}



So, it seems that compiler function bit_test will not work with pointers. I dont really see WHY the compiler does not have support for that, since it is stated that bit_test is the same as ((var & (1<<bit)) != 0) only more efficient. (*myPtr & (1 << bit)) != 0 should work perfectly.

Any suggestions?

//Daniel.[/code]
d00dajo



Joined: 20 Jul 2004
Posts: 34

View user's profile Send private message Send e-mail

Addition.
PostPosted: Tue Sep 20, 2005 1:54 am     Reply with quote

Addition:

((*myPtr & (1<<bitnr)) != 0)

This does work!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 20, 2005 2:03 am     Reply with quote

You didn't give a full test program, so I made one.
This program works OK with PCM vs. 3.233.
It displays:

False

True


Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

struct
{
int8 MyElement[10];
}MyStruct[4];
   
//==============================
void main()
{
int8 *myPtr;

myPtr = &(MyStruct[2].MyElement[6]);


MyStruct[2].MyElement[6] = 0;

if(bit_test(*myPtr,0))
   printf("True\n\r");
else
   printf("False\n\r");
   

MyStruct[2].MyElement[6] = 0xFF;

if(bit_test(*myPtr,0))
   printf("True\n\r");
else
   printf("False\n\r");

while(1);
}
d00dajo



Joined: 20 Jul 2004
Posts: 34

View user's profile Send private message Send e-mail

Problem resolved
PostPosted: Tue Sep 20, 2005 2:24 am     Reply with quote

PCM programmer wrote:
You didn't give a full test program, so I made one.
This program works OK with PCM vs. 3.233.
It displays:

False

True


Hi "PCM programmer",

Thank you for your assistance,

Yes, of course the bit_test function works perfectly with pointers. The problem was derived from a memory overallocation on my part.

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