![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
d00dajo
Joined: 20 Jul 2004 Posts: 34
|
Bit_test function and pointers |
Posted: Tue Sep 20, 2005 1:46 am |
|
|
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] |
|
![](templates/subSilver/images/spacer.gif) |
d00dajo
Joined: 20 Jul 2004 Posts: 34
|
Addition. |
Posted: Tue Sep 20, 2005 1:54 am |
|
|
Addition:
((*myPtr & (1<<bitnr)) != 0)
This does work! |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 20, 2005 2:03 am |
|
|
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);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
d00dajo
Joined: 20 Jul 2004 Posts: 34
|
Problem resolved |
Posted: Tue Sep 20, 2005 2:24 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|