View previous topic :: View next topic |
Author |
Message |
Will Reeve Guest
|
casting to a int1 |
Posted: Fri Jun 06, 2003 3:27 am |
|
|
I was just wondering:
int1 fred;
int16 b;
b = 0x80;
fred = (int1)(b);
is fred == 1 after this line.
Will
P.S. Away from my hardware at the moment so can't check myself!
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515058 |
|
|
R.J.Hamlett Guest
|
Re: casting to a int1 |
Posted: Fri Jun 06, 2003 3:55 am |
|
|
:=I was just wondering:
:=int1 fred;
:=int16 b;
:=b = 0x80;
:=fred = (int1)(b);
:=
:=is fred == 1 after this line.
:=
:=Will
:=P.S. Away from my hardware at the moment so can't check myself!
Yes.
This is one cast, that the CCS compiler does quite nicely. Generally, it does an 'or' on the larger value, and sets/resets the target bit according to whether this is zero or not.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515059 |
|
|
Will Reeve Guest
|
Re: casting to a int1 |
Posted: Fri Jun 06, 2003 9:22 am |
|
|
Thanks, just the answer I was hopeing for!
Cheers,
Will
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515070 |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Does it still work that way? |
Posted: Wed Oct 08, 2008 12:44 pm |
|
|
Hm, I wonder if the ‘OR’ still works in the newer versions of the compiler?
I've got different values of bLoop after
Code: | bLoop = (int1)(input_PCA9536() & APPLICATOR_DETECT); // always zer0 |
and
Code: | bLoop = ((input_PCA9536() & APPLICATOR_DETECT) > 0); |
were different (all inputs being equal). APPLICATOR_DETECT = 0x02 and input_PCA9536() returns a byte.
PCWH 4.071, PIC18LF4525 _________________ Read the label, before opening a can of worms.
Last edited by kender on Wed Oct 08, 2008 3:23 pm; edited 1 time in total |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Oct 08, 2008 1:20 pm |
|
|
If you regard int1 as a boolean type according to the C standard (the only plausible understanding, I think) than the below rule from C standard applies:
Quote: | When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1. |
However, CCS C V3 as well as V4 have some problems with type casts to boolean, e. g. when using integer values with logical operators. An expression as (val1 && val2) clearly requires a type conversion of each operand to boolean followed by a logical and. But this doesn't always work with CCS. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 08, 2008 1:40 pm |
|
|
Quote: | Hm, I wonder if the ‘OR’ still works in the newer versions of the compiler? |
Your posted code shows bitwise 'AND' operations, not 'OR'.
Also, post the value returned by input_PCA9536(). |
|
|
Ttelmah Guest
|
|
Posted: Wed Oct 08, 2008 2:47 pm |
|
|
It is worth reading the documentation that comes with the compiler.
Readme.txt, for the current versions. Tail end, you have the statement:
"Mode CCS2:
onebit = eightbits is compiled as onebit = (eightbits != 0)
all other modes compile as: onebit = (eightbits & 1)"
This is the operation performed by the cast.
This was changed as part of the changes to be ANSI compliant.
The old logical test on the whole byte, was implicit in the original C paperwork (as already stated here), but ANSI C, specifies the conversion to only test the least significant bit. It does seem a 'retrograde' step (the old form was quite useful, and logical), but this is why the behaviour has changed.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Oct 08, 2008 5:23 pm |
|
|
Thank you for pointing to the compilation modes. I didn't yet find a ANSI C specification, that explicitely requires the treatment of the type conversion to int1 as done since CCS V3, but I see, that it can be regarded as a consistent result of giving up the C99 boolean type and the respective special arrangements.
I also have to correct my above statement in so far, that the (val1 && val2) expression is treated correct by CCS C normally. The operands are not casted to int1, the expression is treated equivalent to ((val1 != 0) && (val2 != 0)) as it should be. There is a problem with logical expressions involving bit fields, but it isn't related to the present discussion, as far as I see. |
|
|
|