View previous topic :: View next topic |
Author |
Message |
iso9001
Joined: 02 Dec 2003 Posts: 262
|
Can't figure why bitwise op hangs the pic |
Posted: Tue Sep 14, 2004 9:10 pm |
|
|
Hi.... I'm confused as to why this function hangs the pic up. I'm using an older version of ccs that I know has problems with the bit_set command, so instead of trying my luck with bit_test, I just wrote in the bitwise ops.
This function is supposed to take in an array of 3 bytes and if the bit it is currently checking is a 1 it records a 1... And a 0 gets a 0
I just cant figure out whats wrong with this thing. It just hangs the pic here.
Code: |
void test(int data[]) {
for (ct=0; ct<3; ct++)
for (i=7; i>=0; i--) {
if ((data[ct] & (1<<i)))
checker[x]=1;
else
checker[x]=0;
x++
}
}
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 14, 2004 9:36 pm |
|
|
Most likely it is because you have declared i as an int which is unsigned by default so i>=0 is always true. |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Tue Sep 14, 2004 10:06 pm |
|
|
Remember in CCS the variables are unsigned by default. You have to add the keyword 'signed' if you want them to cover the negative range as well. |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Wed Sep 15, 2004 12:06 am |
|
|
Code: |
void test (int8* Data, int ByteCnt)
{
int8 Shift,BitCnt,i;
i = 0;
do {
Shift = Data[i];
BitCnt = 8;
do {
if(Shift & 0x80)
checker[x] = 1;
else
checker[x] = 0;
Shift <<= 1;
x++;
} while (--BitCnt);
} while (++i < ByteCnt);
}
|
|
|
|
Guest
|
|
Posted: Wed Sep 15, 2004 1:42 am |
|
|
Ah..... stupid unsigned ints...
Thanks everyone!
btw: Dvsoft, what is that code doing ? I dont understant the & 128 part of it... Oh wait, I see... Is it comparing the first bit and then shifting everything over one so that the 2nd bit is now the first? |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Wed Sep 15, 2004 3:39 am |
|
|
bonjour,
yes, you understood
Alain |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Wed Sep 15, 2004 6:08 am |
|
|
I would suggest you define your own data types to avoid this problem. For example:
typedef unsigned int8 UBYTE;
typedef unsigned int8 UINT;
typedef signed int8 INT;
typedef char CHAR;
typedef unsigned int16 UWORD;
typedef signed int16 WORD;
typedef unsigned int32 UDWORD;
typedef signed int32 DWORD;
typedef float FLOAT;
Trampas |
|
|
Guest
|
|
Posted: Wed Sep 15, 2004 7:36 am |
|
|
Trampas wrote: | I would suggest you define your own data types to avoid this problem. For example:
typedef unsigned int8 UBYTE;
typedef unsigned int8 UINT;
typedef signed int8 INT;
typedef char CHAR;
typedef unsigned int16 UWORD;
typedef signed int16 WORD;
typedef unsigned int32 UDWORD;
typedef signed int32 DWORD;
typedef float FLOAT;
Trampas |
ooooops, don't forget one more thing ...
#case // case sensitive !
otherwise, INT will be the same as int ? ? ? I can't remember ...
btw,
#TYPE
Syntax:
#type standard-type=size
Elements:
standard-type is one of the C keywords short,int or long. size is 1,8,16 or 32
Purpose:
By default the compiler treats SHORT as one bit, INT as 8 bits and LONG as 16 bits. The traditional C convention is to have INT defined as the most efficient size for the target processor. This is why it is 8 bits on the PIC�. In order to help with code compatibility a #TYPE directive may be used to will allow these types to be changed. #TYPE can redefine these keywords.
Note that the commas are optional. Since #TYPE may render some sizes inaccessible (like a one bit int in the above) four keywords representing the four ints may always be used: INT1, INT8, INT16 and INT32. Be warned CCS example programs and include files may not work right if you use #TYPE in your program.
Examples:
#TYPE SHORT=8, INT=16, LONG=32 |
|
|
Guest Guest
|
C99 Fixed Width Standard Definitions |
Posted: Fri Sep 17, 2004 8:22 am |
|
|
Don't want to cause a storm here, but what about the ANSI C99 fixed width types - CCS C does accept the following C99 definitions.
Code: |
/* C99 Fixed width definitions */
typedef signed int8 int8_t;
typedef unsigned int8 uint8_t;
typedef signed int16 int16_t;
typedef unsigned int16 uint16_t;
typedef signed int32 int32_t;
typedef unsigned int32 uint32_t;
static union
{
char int8_t_incorrect [sizeof( int8_t) == 1];
char uint8_t_incorrect [sizeof( uint8_t) == 1];
char int16_t_incorrect [sizeof( int16_t) == 2];
char uint16_t_incorrect [sizeof(uint16_t) == 2];
char int32_t_incorrect [sizeof( int32_t) == 4];
char uint32_t_incorrect [sizeof(uint32_t) == 4];
};
|
|
|
|
|