:: Re: [DNG] [OT] [Re: Studying C as t…
Página Principal
Delete this message
Reply to this message
Autor: Edward Bartolo
Data:  
Para: KatolaZ, dng
Assunto: Re: [DNG] [OT] [Re: Studying C as told. (For help)
Hi,

I wrote:
<<
This is the previous version compressed by using another && operand in
the boolean expressions. The final sub-expression is an assignment,
which always evaluates to true provided the other two sub-expressions
evaluate to true. The final statement uses a similar strategy to
compress statements. The key is that a chain of && statements must be
always fully evaluated for the result of the whole expression to be
known. A chain of || only need a sub-expression to evaluate to true
for the result of the whole expression to be known.
>>


I studied 'and' and 'or' boolean operators back when I studied Delphi
Pascal. I am embarrassed to have written such a stupid thing. It must
be due to the fact I wrote the email just before going to bed.

AND: requires ALL sub-expressions to be true to evaluate to true
i.e.   Q = A ^ B ^ C ^ .... D
        Q = 1 if and only if A = B = C = .... D = 1


OR: only requires ONE sub-exression to be true to evaluate to true
i.e.   Q = A v B v C v .... D
        Q = 1 if any Ai = 1


These properties are used by compilers to optimize on boolean
expression evaluation. If I remember well, there are also specific
algorithms to optimize boolean evaluation. I can mention boolean
expression simplification techniques as used to minimize the number of
gates used in logic circuits.

Thanks, Edward

On 21/06/2016, KatolaZ <katolaz@???> wrote:
> On Mon, Jun 20, 2016 at 10:12:40PM +0200, Edward Bartolo wrote:
>
> [cut]
>
>> int main (int argc, char *argv[]){
>>
>>     int c,d=0;
>>     while((c=getchar()) != EOF){
>>         c=putchar(c);
>>         while(c == ' ' && (c=getchar()) != EOF && (d=1));
>>         if (d && putchar(c) && (d=0));
>>     }
>> }

>> >>
>>
>> This is the previous version compressed by using another && operand in
>> the boolean expressions. The final sub-expression is an assignment,
>> which always evaluates to true provided the other two sub-expressions
>
> No, this last sentence is not correct. The value of an assignment
> expression is the value of the right-hand-side of the assignment, as
> you can verify by running this little program:
>
> ====
>
> #include <stdio.h>
>
> int main(){
>         int c;
>         if (c = 0){
>        printf("tadaaaa!\n");
>     }
> }

>
> ====
>
> Note the condition of the if statement (c=0 and not c==0)...
>
>> evaluate to true. The final statement uses a similar strategy to
>> compress statements. The key is that a chain of && statements must be
>> always fully evaluated for the result of the whole expression to be
>> known. A chain of || only need a sub-expression to evaluate to true
>> for the result of the whole expression to be known.
>
> Also this is false. Each element of a chain of "&&" is evaluated until
> one of the elements evaluate to FALSE (0), since in that case the
> result of the && is known (and is equal to 0). The dual happens for
> expressions chained through ||: each of them is evaluated until there
> is one that evaluates to TRUE (!=0), because at that point the result
> of the chain is known (and is equal to 1).
>
> This is called "operator short-circuiting", and can give rise to
> pretty unreadable code in C...
>
> HND
>
> KatolaZ
>
> --
> [ ~.,_  Enzo Nicosia aka KatolaZ - GLUGCT -- Freaknet Medialab  ]
> [     "+.  katolaz [at] freaknet.org --- katolaz [at] yahoo.it  ]
> [       @)   http://kalos.mine.nu ---  Devuan GNU + Linux User  ]
> [     @@)  http://maths.qmul.ac.uk/~vnicosia --  GPG: 0B5F062F  ]
> [ (@@@)  Twitter: @KatolaZ - skype: katolaz -- github: KatolaZ  ]

>