Dima Bilan
Dima Bilan

Reputation: 9

small letter to large java example Shildt

char ch;

for (int i = 0; i < 10; i++) {
    ch = (char) ('a' + i);
    System.out.print(ch); //<- output small letter
    ch = (char) ((int) ch & 65503); //<-- ???
    System.out.print(ch + "  ");
}

Please explain the part of code ch = (char) ((int) ch & 65503);

Why do we need 65503 for this?

Upvotes: 0

Views: 343

Answers (1)

Dmitry
Dmitry

Reputation: 7266

Java uses UTF-16 characters. These characters take 2 bytes. The number 65503 has the bit pattern of

1111 1111 1101 1111
            ^       // only the 6th bit is 0

As you can see only the 6th bit is 0. When you use this number as a bit-mask and do a bit-wise AND operation (your char & 65503) it turns the 6th bit of that char off (make it a zero).

If you look at the ASCII table (ASCII and Unicode are the same for the Latin alphabet) you'll see that that 6th bit is the difference between capital and none-capital Latin letters (the difference in values is exactly 32). So if you have small Latin characters they will turn into capital character. If you have capital characters it will do nothing, because their 6th bit is already 0. So basically it's a hack to do toUpperCase() on Latin characters.

Upvotes: 1

Related Questions