bracketing confusion :)

I once ran into a basic ROT-13 encryption algorithm.
In there I found this line

byte = ((byte >= 'A') && (byte <= 'Z') ? ((byte - 'A' + 13) % 26 + 'A') : byte) | cap;

where

register char byte, cap;

that to my astonishment keeps compiling (no errors).

I don't understand the last expression in the ternary operation.
I still see an orphan bracket there after the colon : byte) | cap;
Can someone maybe be so kind as to explain how the syntax above works?
Last edited on
That looks like a perfectly valid line of C++ code to me. Perhaps your eyes are playing tricks on you.

byte = ((byte >= 'A') && (byte <= 'Z') ? ((byte - 'A' + 13) % 26 + 'A') : byte) | cap;

The result of the ternary operator expression is contained within parens. There are no bracket operators in this expression.
Last edited on
OK then can I ask what the closing bracket after the colon ")" couples with?
: byte) | cap;
Would you mind highlighting it for me?

Yes I now see it thanks very much.
I now understand the result of the ternary operation expression
is being OR'ed with cap and the bit possibly getting switched on?

Never had seen a ternary operation in brackets before gosh.
Last edited on
I already highlighted the open and close parenthesis in bold. Take another look at what I posted. The result of the ternary operator expression is going to be logically ORed to the value contained by cap. Try counting the parens and you will see that they add up to 5 of each.
Yes thanks again I got stuck on this almost two years ago what a relief.
So an official version of the ROT-13 algorithm follows just so it all makes sense to everybody.
Apparently in the C subset.

Cheerio.

1
2
3
4
5
6
7
8
9
10
11
int main ()
{
  register char byte, cap;
  for(;read (0, &byte, 1);)
    {
      cap = byte & 32;
      byte &= ~cap;
      byte = ((byte >= 'A') && (byte <= 'Z') ? ((byte - 'A' + 13) % 26 + 'A') : byte) | cap;
      write (1, &byte, 1);
    }
}
Last edited on
Topic archived. No new replies allowed.