|
In this webpage, I will review (maybe expose ?) some arithmetic operations on ASCII codes
ASCII codes can be used in 2 different ways:
|
Commonly used computations using ASCII codes:
|
DEMO: /home/cs255001/demo/ASCII-code/charArith.java
What decimal integer value is stored in the int variable i
int i; i = '2' - '0'; // i = 00000....0000010 bin = 2 decimal i = 10 * i; // 10 decimal = 00...001010 bin |
What decimal integer value is stored in the int variable i
int i; i = '2' - '0'; // i = 00000....0000010 bin = 2 decimal i = 10 * i; // 10 decimal = 00...001010 bin |
Answer: the computer makes sure that the computation is performed in binary !
000...00001010 * 000...00000010 = 00000000000000000000000000010100 = 20(10) |
DEMO: /home/cs255001/demo/ASCII-code/Quiz_3.java
What is printed by the following Java program:
s = "12"; s = s + '3'; System.out.println(s); |
What is printed by the following Java program:
s = "12"; s = s + '3'; System.out.println(s); |
Answer:
123 (because we concatenate "12" and '3' -> "123") |
DEMO: /home/cs255001/demo/ASCII-code/Quiz_4.java
What is printed by the following Java program:
s = "12"; s = s + (char)(3 + '0'); System.out.println(s); |
What is printed by the following Java program:
s = "12"; s = s + (char)(3 + '0'); // (char)(3 + 48) = (char)51 = '3' System.out.println(s); |
Answer:
123 (because we concatenate "12" and '3' -> "123") |
DEMO: /home/cs255001/demo/ASCII-code/Quiz_4.java
What is printed by the following Java program:
s = "12"; s = s + (char)(3 + 48); System.out.println(s); |
What is printed by the following Java program:
s = "12"; s = s + (char)(3 + 48); // (char)(3 + 48) = (char)51 = '3' System.out.println(s); |
Answer:
123 (because we concatenate "12" and '3' -> "123") |
DEMO: /home/cs255001/demo/ASCII-code/Quiz_4.java
What is printed by the following Java program:
s = "12"; s = s + (3 + 48); System.out.println(s); |
What is printed by the following Java program:
s = "12"; s = s + (3 + 48); // "12" + 51 ===> "12" + "51" = "1251" System.out.println(s); |
Answer:
1251 (because Java first converts 51 to "51" and then concatenates "12"and "51" -> "1251") |
DEMO: /home/cs255001/demo/ASCII-code/Quiz_4.java