Review: computing with ASCII code as binary numbers

  • A character in Java programs is represented by is ASCII code (binary number)

    Example:

          'A'  ≡  01000001    (= 65(10))
      
          '0'  ≡  00110000    (= 48(10))
          '1'  ≡  00110001    (= 49(10))
         

  • Important fact:

      • When you write 'A' in a Java program, you are actually writing:

           (char)65 // Value 65 interpreted as an ASCII code 
          

In this webpage, I will review (maybe expose ?) some arithmetic operations on ASCII codes

ASCII codes have 2 (dual) functions in computer programs
 

ASCII codes can be used in 2 different ways:

  1. ASCII codes are designed to represent symbols in the English alphabet

    Example:

      • 00110000 represents the symbol 0
      • 01000001 represents the symbol A           

  2. ASCII codes are also binary numbers

    Therefore: we can use ASCII codes in computations !!!

    Example:

        '0' + 1   ≡  (char)00110000 + (int)00000001  
                  =  00110001 
      

Commonly used computing with ASCII code as binary numbers

Commonly used computations using ASCII codes:

  1. Add the number x to '0' to obtain the ASCII code for the number:

      x = '0';        // x = 00110000 = 48(10)
    
      x = 1 + '0';    // x = 00110001 = 49(10)
          // Converts 2s compl code for 1 => ASCII code for 1
    
      x = 4 + '0';    // x = 00110100 = 52(10)
          // Converts 2s compl code for 4 => ASCII code for 4 

  2. Subtract an ASCII (digit) code from '0' to obtain the 2s complement code for the ASCII (digit) code:

      x = '1' - '0';    // x = 00000001 = 1(10)
          // Converts ASCII code for 1 => 2s compl code for 1
      x = '4' - '0';    // x = 00000100 = 4(10)
          // Converts ASCII code for 4 => 2s compl code for 4     

DEMO: /home/cs255001/demo/ASCII-code/charArith.java

Quiz
 

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
   

Quiz
 

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

The string concatenation operator (+) in Java
 

What is printed by the following Java program:

   s = "12";

   s = s + '3';

   System.out.println(s);
   

The string concatenation operator (+) in Java
 

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

The string concatenation operator (+) in Java
 

What is printed by the following Java program:

   s = "12";

   s = s + (char)(3 + '0');  

   System.out.println(s);
   

The string concatenation operator (+) in Java
 

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

The string concatenation operator (+) in Java
 

What is printed by the following Java program:

   s = "12";

   s = s + (char)(3 + 48);  

   System.out.println(s);
   

The string concatenation operator (+) in Java
 

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

The string concatenation operator (+) in Java
 

What is printed by the following Java program:

   s = "12";

   s = s + (3 + 48);  

   System.out.println(s);
   

The string concatenation operator (+) in Java
 

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