Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let us take an example to understand your code. </p> <p>1) Suppose you pass the character 'p' to your method <b>toUpper()</b>.</p> <p>2) Now the condition in your if-statement will be always true since any alphabet is between 'a' and 'z'.</p> <p>3) Inside if-statement you have the following code</p> <pre><code>return (char) (c &amp; ~('a' - 'A')); </code></pre> <p>4) In above statement this part</p> <pre><code>('a' - 'A') </code></pre> <p>will be always executed first since it is in parenthesis. Here you are just subtracting 'A' from 'a' i.e. 97-65 which are ASCII values. (A---> 65 and a----> 97). So the answer would be always 32 regardless of the character you pass to your <b>toUpper()</b> method.</p> <p>5) How then there is the operator <b>~</b> i.e.</p> <pre><code>~('a' - 'A') </code></pre> <p>As I told the answer of ('a' - 'A') would be always 32, therefore the operator ~ is applied on the number 32 i.e.</p> <pre><code>~32 </code></pre> <p>To predict the output of the operator ~, the formula is as shown below</p> <p><i> ~(number)<br> = -(number) - 1 </i><br><br>Since here the number is 32 therefore the output of ~32 from above formula is </p> <p><i> -(32) - 1 <br> = -32 - 1 <br>= -33</i></p> <p>So the output of </p> <pre><code>~('a' - 'A') </code></pre> <p>would be always -33</p> <p>5) Now you have </p> <pre><code>(c &amp; ~('a' - 'A')) </code></pre> <p>i.e</p> <pre><code>(c &amp; -33) </code></pre> <p>Here c has the alphabet which user passes, in our example it is 'p'</p> <p>i.e.</p> <pre><code>p &amp; -33 </code></pre> <p>Since the ASCII value of 'p' is 112 i.e</p> <pre><code>112 &amp; -33 </code></pre> <p>i.e.</p> <pre><code>1110000 &amp; 1011111 </code></pre> <p>which are the corresponding value of 112 and -33 in binary</p> <p>So after applying the operator &amp; we get</p> <pre><code>1010000 </code></pre> <p>6) Now convert 1010000 into decimal we get 80 which is the ASCII value of uppercase alphabet 'P'.</p> <p>7) So in general we can say the operation that is going to be performed is</p> <pre><code>(ASCII value of user inputted alphabet) &amp; -33 </code></pre> <p>8) One more thing "Java goes for Unicode. But the first set of characters in Unicode are ASCII, also said by @paxdiablo. So I have mentioned ASCII in above answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload