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 CCS Technical Support

Little help, please
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Wed Jun 12, 2013 8:01 am     Reply with quote

Jean,

Your two pieces of code are not doing exactly the same thing.

The first one you show. It wants to receive 2 identical characters before it checks the bits and sets your flags. So to get it to work you e.g. send 0x05 0x05 and it should set B_MI true. Is that what you intended?
Jean FOUGERON



Joined: 30 Nov 2012
Posts: 110
Location: France

View user's profile Send private message Visit poster's website

PostPosted: Thu Jun 13, 2013 2:11 am     Reply with quote

Hello OXO

The line
Code:
B_MI=(I_Receive[0]&0x04);
is replaced by the line
Code:
switch (I_Receive[0])
         {
            case 0x05:
               B_MI=TRUE;
               break;
            case 0x0D:
               B_MI=TRUE;
               break;
            case 0x25:
               B_MI=TRUE;
               break;
            case 0x2D:
               B_MI=TRUE;
               break;
            default:
               B_MI=FALSE;
               break;   
         }


so it has to do the same

I receive 2 time the same character (normally) and want to test bit 2 or 3 to intend my action.
I do not really understand why the second method works as the first does'nt.
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Jun 13, 2013 2:47 am     Reply with quote

Ok, I got it.

B_MI is a boolean, so 1 bit. You cannot store 0x04 into a single bit.
Jean FOUGERON



Joined: 30 Nov 2012
Posts: 110
Location: France

View user's profile Send private message Visit poster's website

PostPosted: Thu Jun 13, 2013 2:51 am     Reply with quote

Sorry
I understand this line
Code:
B_MI=(I_Receive[0]&0x04);
as
I mask I_receive[0] with 0x04=0b00000100, so it only stays bit 2
if bit 2 is set, B_MI = TRUE, if not B_MI=FALSE

Where is my error ?
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Jun 13, 2013 2:58 am     Reply with quote

Your line
Quote:
B_MI=(I_Receive[0]&0x04);


evaluates to
..
Code:
B_MI=( 0x05 & 0x04);

..
Code:
B_MI=(0x04);


If you used
Code:
B_MI= ( (I_Receive[0]&0x04) != 0 );


Then it would generate a true/false which will store into a 1 bit boolean.
Jean FOUGERON



Joined: 30 Nov 2012
Posts: 110
Location: France

View user's profile Send private message Visit poster's website

PostPosted: Thu Jun 13, 2013 3:18 am     Reply with quote

True, it works
Thanks

and ...
Code:
B_MI=(Boolean)(I_Receive[0]&0x04);

does not, is there a reason ?
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Jun 13, 2013 3:22 am     Reply with quote

From the help docs:

Quote:
The compiler provides automatic type conversions when an assignment is performed. Some information may be lost if the destination can not properly represent the source. For example: int8var = int16var; Causes the top byte of int16var to be lost.


So your cast of an int8 to a boolean loses the top 7 bits.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Jun 13, 2013 3:49 am     Reply with quote

In C there is no boolean type. Boolean values, true and false, are represented by integers: 0 is "false", anything else is "true". It doesn't matter what length the integers are. In CCS "TRUE" and ""FALSE" are simply defines equating to the integer literals, 1 and 0. Also, what appears to be a "BOOLEAN" type is also simple define to int1, not even a typedef. They are in the processor .h file.

int1s are not booleans, they are just single bit length integers and operations done with them, including assignment, are done as if they were numbers, not logical values. That's why there are logical and bitwise versions of some operators such as & (bitwise and) and | (bitwise or), and && (logical and) and || (logical or). The bitwise versions treat the operands as numbers: bit patterns, while the logical versions treat them as "zero is false, anything else is true" "booleans". C compilers don't know you mean them to treat the result as a logical value when you try to assign a longer int to a smaller one. C will simply truncate as it always does with integers, and assign only those least siginificant bits that fit in the smaller integer.

Even a cast won't work as there is NO implicit boolean type in C: C doesn't know the "rules" for booleans, except in statements that expect logical values, such as comparisons, e.g. if and while and part of for or when you use an explicit logical function operator such as ==, && etc..

In other more practical form:

Code:

int1 Int1_Result, Bool_Result;
unsigned int8 Int8_Result;
unsigned int16 Value = 0x0ABC;

Int8_Result = Value;
Int1_Result = Value;
Bool_Result = (Value != 0);


// Int8_Result will be 0xBC
// Int1_Result will be 0, i.e. the LSB of 0x0ABC, or 0b0000101010111100.
// Some compilers may warn of the possiblilty of loosing significant bits.
// Bool_Result will be 1 as 0x0ABC is NOT zero, but Bool_Result is still really an integer, albeit a very short one.
// Do not test arbitary integer values against 1, use 0 as "zero is FALSE, anything else is TRUE".


Many other languages, notably newer variants of C such as C++ and C#, have true boolean types, and know the right (logical) operations to use with them. C doesn't and you, the programmer, have to make sure the correct operation is done on values you intend to be interpreted as booleans.

RF Developer
Jean FOUGERON



Joined: 30 Nov 2012
Posts: 110
Location: France

View user's profile Send private message Visit poster's website

PostPosted: Thu Jun 13, 2013 4:02 am     Reply with quote

oxo wrote:

So your cast of an int8 to a boolean loses the top 7 bits.

OK, I understand
Thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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