csc373 Review Questions


  1. Binary-hex-decimal

    Give the requested representations:

    1. 8 bit binary representation of 106. Ans: 64 + 32 + 8 + 2 = 011010102.
    2. decimal representation of 001011002. Ans: 32 + 8 + 4 = 44
    3. decimal representation of 0x0ABC. Ans: 10 * 256 + 11 * 16 + 12 * 1= 444
    4. 8 bit binary representation of 24 Ans: 0001 0000
    5. 8 bit binary representation of 27 Ans:1000 0000
    6. hex representation of 24 Ans:0x10
    7. hex representation of 27 Ans:0x80
  2. Little Endian/Big Endian

    A 4 byte signed integer is stored at locations 0x8000 - 0x8003

                          Stored
                 Address   Byte
    	      0x8000:  0x00 
                  0x8001:  0x01
    	      0x8002:  0x02
                  0x8003:  0x00
    	    
    1. If the machine is little endian, what is the integer value (in decimal)? Ans: (2 * 164 + 1 * 162) = 131328

    2. If the machine is big endian, what is the integer value (in decimal)? Ans: (1 * 164 + 2 * 162) = 66048

  3. C pointers

    If the integer array x begins at address 0x8000 and

     int x[3] = {10, 20, 30};
     int *p;
     int *q;
     p = &x[0];
     q = &x[1];
    	

    give the values of the following expressions:

    1. p
      Ans:0x8000
    2. p + 1
      Ans:0x8004
    3. *p
      Ans:10
    4. *(p + 1)
      Ans:20
    5. *p + 1
      Ans:11
    6. q
      Ans:0x8004
    7. q - 1
      Ans:0x8000
    8. *q
      Ans:20
    9. *(q - 1)
      Ans:10
    10. *q - 1
      Ans:19
  4. C Bit Operations (in binary)

    Give the value of following expressions in binary if the values of a and b in binary are:

     a = 1100
     b = 1010
    	
    1. a & b
      Ans:1000
    2. ~(a & b)
      Ans:0111
    3. ~a
      Ans:0011
    4. ~b
      Ans:0101
    5. ~a | ~b
      Ans:0111
    6. a ^ b
      Ans:0110
    7. a ^ a
      Ans:0000
    8. a | a
      Ans:1100
    9. a | ~a
      Ans:1111
    10. a & ~a
      Ans:0000
  5. ! the 'bang' operator

    Is there a value of the int variable x such that

     x == !!x	  
    	

    Ans: Yes. For example, x = 0.

  6. C bit operations (in hex)

    Give the value of following expressions in hex if the values of a and b in hex are:

     a = 0xC
     b = 0xA
    	
    1. a & b
      Ans:0x8
    2. ~(a & b)
      Ans:0x7
    3. ~a
      Ans:0x3
    4. ~b
      Ans:0x5
    5. ~a | ~b
      Ans:0x7
    6. a ^ b
      Ans:0x6
    7. a ^ a
      Ans:0x0
    8. a | a
      Ans:0xC
    9. a | ~a
      Ans:0xF
    10. a & ~a
      Ans:0x0
  7. Left Shift (in binary)

    For the 1 byte (8 bit) signed integer

    	  char x = 0x6A; // 011010102
    	

    Give the value in hex and binary of the following shift operations:

    1. x << 1 Ans: binary: 11010100, hex: 0xD4
    2. x << 2 Ans: binary: 10101000, hex: 0xA8
    3. x << 3 Ans: binary: 01010000, hex: 0x50
    4. x << 4 Ans: binary: 10100000, hex: 0xA0
  8. Arithmetic Right Shift (in binary and hex)

    For the 1 byte (8 bit) signed integer, assume right shift is arithmetic

    	  char x = 0x6A; // 011010102
    	  char y = 0x8A; // 100010102
    	

    Give the value in hex and binary of the following shift operations:

    1. x >> 1 Ans: binary: 00110101, hex: 0x35
    2. x >> 2 Ans: binary: 00011010, hex: 0x35
    3. y >> 1 Ans: binary: 11000101, hex: 0xC5
    4. y >> 2 Ans: binary: 11100010, hex: 0xE2
  9. Logical Right Shift (in binary and hex)

    For the 1 byte (8 bit) unsigned integer, assume right shift is logical

    	  char x = 0x6A; // 011010102
    	  char y = 0x8A; // 100010102
    	

    Give the value in hex and binary of the following shift operations:

    1. x >> 1 Ans: binary: 00110101, hex: 0x35
    2. x >> 2 Ans: binary: 00011010, hex: 0x35
    3. y >> 1 Ans: binary: 01000101, hex: 0x45
    4. y >> 2 Ans: binary: 00100010, hex: 0x4n2
  10. Write the function f below that returns an integer like y except the low order 2 bytes are 0.

    Example: If y = 0x12AB86C3, the function f should return the value 0x12AB0000.

     int f(int x)
     {
       ...
     }	  
    	

    return y & 0xFFFF0000; or
    return y & (~0 << 16);

  11. Truncation, Sign Extension

    For some initial values of x at line 1, the value of x after line 3 not the same as the initial value.

    There are many possible answers. One answer is 0x00008000. Give two other possible answers.

        1   int x = ?;     // Ans: 0x00008000 
        2   short s = x;   // assigns 0x8000 to s (truncation)
        3   x = s;         // assigns 0xffff8000 to x (sign extension)
    	

    Ans: one answer: 0x0000C000, another answer: 0xffff7000

  12. IEEE floating point

    A 32 bit IEEE float value has hex representation:

     0x40B00000	  
    	

    Give the decimal representation of this float number.

    Ans: 5.5

  13. IEEE floating point types

    Give an 32 bit example (in hex) of each of the following:

    1. positive denormalized
      Ans: 0x00700000
    2. positive normalized
      Ans: 0x00800000
    3. +inf
      Ans: 0x7F800000
    4. NaN
      Ans: 0x7FA00000
  14. Signed/Unsigned comparision

    The sizeof(T) function returns the size in bytes of a value of type T. For example, sizeof(char) is 1, sizeof(short) is 2, sizeof(int) is 4, sizeof(double) is 8. Since the size of any type is never negative, the return type of sizeof is unsigned int.

    What is the output of the following program?

    int main()
    {
      int x;
    
      x = -1;
    
      while( x < sizeof(int) ) {
        x++;
        printf("x = %d\n", x);
      }
      printf("Done\n");
    
      return 0;
    }
    

    Ans: Done