? - Operator issue - checks wrongly

Hi,

I want to avoid if/else, therefore use the ? operator
aA is an int, looped from 0 to 10:

dD=(aA==aA^aA^0)?12:(aA==1)?23:(aA==2||aA==3||aA==9)?34:(aA==4||aA==7)?45:(aA==5)?56:(aA==6)?67:(aA==8)?78:(aA==10)?89:90;

Ouput: :(

1
2
3
4
5
6
7
8
9
10
11
aA = 0 --> dD = 12
aA = 1 --> dD = 23
aA = 2 --> dD = 12
aA = 3 --> dD = 12
aA = 4 --> dD = 12
aA = 5--> dD = 12
aA = 6 --> dD = 12
aA = 7 --> dD = 12
aA = 8 --> dD = 12
aA = 9 --> dD = 12
aA = 10 --> dD = 12


Please help me,

greetings,

Lukas
What does subexpression aA^aA^0 mean? It is always equal to 0.
What values do you think this takes? -> aA == aA ^ aA ^ 0

What values does it actually take?

Useful link -> http://en.cppreference.com/w/cpp/language/operator_precedence
Last edited on
Sorry, yes, I wanted to obfuscate the program, I forgot to remove this bit, yes, it's 0.

dD=(aA==0)?12:(aA==1)?23:(aA==2||aA==3||aA==9)?34:(aA==4||aA==7)?45:(aA==5)?56:(aA==6)?67:(aA==8)?78:(aA==10)?89:90;

is the code that gets executed ... but it doesn't work, does anyone know, why?

This is gonna be a small exercise for ur mind ...
Last edited on
it doesn't work

What do you mean?
Output:

1
2
3
4
5
6
7
8
9
10
11
aA = 0 --> dD = 12
aA = 1 --> dD = 23
aA = 2 --> dD = 12
aA = 3 --> dD = 12
aA = 4 --> dD = 12
aA = 5 --> dD = 12
aA = 6 --> dD = 12
aA = 7 --> dD = 12
aA = 8 --> dD = 12
aA = 9 --> dD = 12
aA = 10 --> dD = 12


Expected Output:

1
2
3
4
5
6
7
8
9
10
11
aA = 0 --> dD = 12
aA = 1 --> dD = 23
aA = 2 --> dD = 34
aA = 3 --> dD = 34
aA = 4 --> dD = 45
aA = 5 --> dD = 56
aA = 6 --> dD = 67
aA = 7 --> dD = 45
aA = 8 --> dD = 78
aA = 9 --> dD = 34
aA = 10 --> dD = 89
Ok. So, what's the problem? -> http://ideone.com/8Iz3dN
Last edited on
WTF !! It doesn't work for me if i choose other values !!

try
 
int dD=(aA==aA^aA^0)?72:(aA==1)?101:(aA==2||aA==3||aA==9)?108:(aA==4||aA==7)?111:(aA==5)?32:(aA==6)?87:(aA==8)?114:(aA==10)?100:33;
I now already spent hours in trying to spot the difference ... i can't !! wtf...

I'm totally confused
> I'm totally confused

What else do you expect with code like this?
dD=(aA==aA^aA^0)?12:(aA==1)?23:(aA==2||aA==3||aA==9)?34:(aA==4||aA==7)?45:(aA==5)?56:(aA==6)?67:(aA==8)?78:(aA==10)?89:90;

I too am totally confused.
I now already spent hours in trying to spot the difference

Since you're having such a hard time spotting the problem, I'll give you one more hint:
Try this -> (aA==(aA^aA^0)) instead of this -> (aA==aA^aA^0) and verify that it works.

Then, read my first post carefully in order to understand why it works.
It'll take forever, but write out the truth table for each of these comparisons for each iteration. It should become apparent what you're doing wrong.
Thank ya, m4ster !1!! *takes a bow and kisses your feet, blabla*
Your first post?

What values do you think this takes? -> aA == aA ^ aA ^ 0

What values does it actually take?

Useful link -> http://en.cppreference.com/w/cpp/language/operator_precedence


Hmmm... At first I couldn't use this link, but do ya mean "from left to right" of the link? :O

Thanks, THAT is somehow the difference ... :( I'm ashamed (Do I have to? I guess so...)

I too am totally confused.

Thanks soooo much for this comment,JLBorges ,I'm feeling a bit better now :D

Lukas
Last edited on
What I mean is that the == operator has higher precedence than the ^ operator.

It follows that this -> (aA == aA ^ aA ^ 0) is the same as this -> ((aA == aA) ^ aA ^ 0), which is the
same as this -> (1 ^ aA ^ 0), which is false if aA is 1 and true otherwise, which is not what you want.
aaaaah, k, thx for clarifying :)
Topic archived. No new replies allowed.