0

Hi Please somebody can explain me the meaning of this conditional statement? This is a java code.

(chaIntVal >= 0x10 ? chaIntVal  : chaIntVal | 0x60)
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
user3022123
  • 47
  • 1
  • 9
  • 1
    Well it's just a conditional operator expression. It's not a statement in its own right. Which part of it are you finding hard to understand? Is it the bitwise `or`? – Jon Skeet Nov 29 '13 at 08:30
  • 1
    possible duplicate of [What is the Java ?: operator called and what does it do?](http://stackoverflow.com/questions/798545/what-is-the-java-operator-called-and-what-does-it-do) – AJ. Nov 29 '13 at 08:32
  • @JonSkeet, Yes I don't understand the | gate in middle. – user3022123 Nov 29 '13 at 08:39
  • @user3022123: Then I don't understand how the accepted answer has helped you, as that's only explained the conditional operator part... – Jon Skeet Nov 29 '13 at 09:20
  • @JonSkeet, It was hard to find exact answer helped me. But the accepted answer helped me to understand and with your answer too. So how can I mark yours as well as the answer? – user3022123 Nov 30 '13 at 14:04
  • You can't - I didn't answer, as your question didn't make it clear what you didn't understand. – Jon Skeet Nov 30 '13 at 14:12

4 Answers4

2

It means:

int res;
if(chaIntVal >= 0x10) {
    res = chaIntVal;
} else {
    res = chaIntVal | 0x60;   // binary or
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

It is a ternary operator which means this.

condition ? value_if_true : value_if_false;

It is almost equivalent to

if(condition){
    // when true do this
}else{
    // when false do this
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

This expression returns chaIntVal as is if chaIntVal > 16; otherwise it will set bits 5 and 6 to 1 of chaIntVal (binary OR http://www.xcprod.com/titan/XCSB-DOC/binary_or.html) and returns it.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

If the high bits after the 4 low bits of the value are not set (value in range 0-15), do set bits x11x xxxx.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138