Help with a programming line

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,366
EDIT:
Hi,
can someone help me understand this:

Flag = ( ui8 )((Var_0 == 2) && (Var_1 == 1);

I understand if the "&&" was bitwise "&", then it makes more sence. But how can a flag equal a condition? Maybe this assign was ment to be compare?

I don't know in which programming language is this. Does it make a difference? I was assigned this task to figure it out.
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,273
And again if C, (ui8) would be a cast to what is probably an 8-bit unsigned int, which would be a reasonable type for "Flag", especially on a smaller processor. There is a syntax error, it's missing a closing ) by the ;.
 

MrSoftware

Joined Oct 29, 2013
2,273
I am no C expert, but that statement seems incorrect to me.

I would have said:
In C, false is zero and true is anything other than zero
Actually, starting in C99, the native type is _Bool which is typically mapped to "bool", and it can hold 0 or 1. I don't think "true" and "false" are actually part of the spec, but they are typically mapped to 1 and 0. For example:

upload_2018-12-13_9-30-2.png

upload_2018-12-13_9-32-25.png
 

hexreader

Joined Apr 16, 2011
619
That's true the other way round. In C zero is false and anything else is true.

uint_8 a1 = (3 != 4);

Here a1 will be the value of true (as 3 is not equal to 4). a1 will be 1.
Nice catch, That is probably what I meant to say, but you put it much better :)

As for true/false, I guess in my head, I am not thinking in terms of literally true/false, but more in terms of whether an "if" expression passes or or not. Bool is easy, since there are only two possibilities, but I see people test char/int etc in this manner:

signed int myvar = 3;
if(myvar){
// will always get here since 3 passes if test
}
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,273
You are correct that non-zero values evaluate to true inside statements like if(). I am thinking that under the hood, the expression is implicitly cast to _Bool before being evaluated. Maybe someone with low level compiler knowledge, or specification knowledge can chime in :)
 

AlbertHall

Joined Jun 4, 2014
12,625
I'm trying to avoid C99. When I reinstalled the PIC C compiler XC8, it decided to use C99 and suddenly quite a few of my programs produced weird compile errors. It took me a while to discover the problem.
 

djsfantasi

Joined Apr 11, 2010
9,237
But in this line, non-zero and non-one values don’t occur.

If Var_0 is equal to 2, the parentheses evaluate to 1; 0 otherwise. The same analysis applies to Var_1! And the final operation is also either 0 or 1. There is no possibility of a result other than 0 or 1.

The (uint_8) simply casts the result into an 8 bit unsigned integer, but it’s values can either be, wait for it, 0 or 1.
 

WBahn

Joined Mar 31, 2012
32,833
You are correct that non-zero values evaluate to true inside statements like if(). I am thinking that under the hood, the expression is implicitly cast to _Bool before being evaluated. Maybe someone with low level compiler knowledge, or specification knowledge can chime in :)
Depends on the compiler. The C language standard was written specifically to give the implementer as much flexibility as possible in crafting efficient implementations. The language standard says that a 0 (or a value, like NULL, that is specified to behave like a 0) is a logical false while any other value is a logical true. The generated code can thus simply check if the value is zero and do the false-stuff else do the true stuff.

The C language standard also states that the result of evaluating a logical expression is a 0 if false and a 1 if true -- no other options there.

These constraints give you great flexibility in mixing logical and other expressions to make powerful, compact, and efficient constructs. It also makes it easy to make code that is hard to read for a human.
 

Thread Starter

ArakelTheDragon

Joined Nov 18, 2016
1,366
Thanks for all the replies, they are useful.

I accept this as the answer:
AlbertHall said:
It looks like C but could possibly be something else.
Assuming it's C, The && is a logical AND comparison - true if both operands are true.
In C true is 1 and false is 0.
So Flag will be 1 if Var_0 is 2 and Var_1 is 1, otherwise it will be 0.
As for the "C99" problem, it most likely comes from the XC8 use of an "all-catch" header file (xc.h or htc.h I think).

Normally the "C" standard does not define the variables. They are left to the compiler. But it is recommended to use the standard "C99" types as that makes it easier to port/understand and program.
 
I'm not so sure it is C. Consider that it might be JavaScript and the '==' is loose equality operator, in contrast to the
Strict equality '===' In which case the 'answer' is different. But, why are you concerned with the statement if you are unsure of what language it is using? Did I miss that somewhere?


 
Top