3iff wrote:
Another question.
Actually, it's three questions, but never mind
3iff wrote:
(LOAD_PERM(1) > SOMEVALUE ? 1 : 0)
Does this mean if the value of LOAD_PERM(1) is greater than SOMEVALUE then it returns 1 otherwise it returns zero?
That is what it means.
3iff wrote:
and what is the meaning of the ? symbol. It's been puzzling me for a while.
It's the ternary ?: operator. Its form is "<cond> ? <true-val> : <false-val>" which is similar to "if <cond> then return <true-val> else return <false-val> end", except the ?: form is an expression and not a statement, thus ((x > 1) : 1 : 2) + y works.
The ternary operator comes from C/C++.
3iff wrote:
And another one that I cannot even guess at...
(VALUE) / (1 << 21) - what does (1 << 21) mean?
The << means 'shift to the left', (there is also >>, which means 'shift to the right). In the binary system, shifting is logical (see below). In the decimal system that we normally use, "x << 1" is equal to "x * 2", x << 2 = (x << 1) << 1, so 1 << 21 thus means 1 * 2 * 2 * ... * 2 (=1 * 2^21 = 2097152).
The more sane explanation is if you don't use the decimal system, but switch to binary numbers instead. Decimal value 1 in the binary system is 1 or 00000000000000000000000000000001 for a 32 bit number (just as with the decimal system, adding prefix zeroes is allowed, 5 == 0000005).
In the binary system "shifting" means literally shifting.
1 = 1 << 0 = 00000000000000000000000000000001, 1 << 1 = 00000000000000000000000000000010, 1 << 2 = 00000000000000000000000000000100, etc. The '1' shifts to the left each time.
1 << 21 is thus 00000000001000000000000000000000.
An this is what it is used for normally. It allows for very compact notation of bit patterns, especially when you use hexadecimal numbers (since four 0/1 bits become a single digit then). It takes some getting used to however.
While 'shifting' may be a new term for you, the concept itself also exists in the decimal system, it is known as 'exponent'. we write 2000 as 2e03 or 2e3 (= 2*10^3).