Eroxl's Notes
01 - Numbers and Memory (CPSC 213 Practice)

Problem 1

How many bytes are used for a variable of type char in C?

  • [ ] (a) 1
  • [x] (b) 2
  • [ ] (c) 3
  • [ ] (d) 4
  • [ ] (e) 5
  • [ ] (f) 6
  • [ ] (g) 7
  • [ ] (h) 8
  • [ ] (i) More than 8

Problem 2

Is the address 0x00009661 aligned to store a 8-byte integer?

  • [ ] Yes
  • [x] No

Problem 3

Consider the following content of a portion of memory (in the form of address: value):

0x1000: 0xbe
0x1001: 0xe2
0x1002: 0x0f
0x1003: 0x61
0x1004: 0x42
0x1005: 0x49
0x1006: 0x84
0x1007: 0xf4

What is the big-endian value of the 4-byte integer in hexadecimal at address 0x1004?

0x424984f4

Problem 4

Consider a C program running on a big endian processor with the following variable declaration.

int a = 28;

Assume that the memory address of the variable a is 0x2000.

Indicate which of the following statements are true immediately following the execution of this statement.

Note that in some questions the format address: value shows the value of individual bytes.

  • [ ] The char value at address 0x2003 is equal to the numerical value of 0x00000000
  • [x] The char value at address 0x2003 is equal to the numerical value of 0x0000001c
  • [ ] The char value at address 0x2000 is equal to the numerical value of 0x0000001c
  • [x] Memory stores:
0x2000: 0x00
0x2001: 0x00 
0x2002: 0x00 
0x2003: 0x1c
  • [x] The int value at address 0x2000 is 0x0000001c
  • [ ] Memory stores:
0x2000: 0x00
0x2001: 0x00 
0x2002: 0x00 
0x2003: 0xc1

Select all possible options that apply.

Problem 5

Consider the following content of a portion of memory (in the form of address: value)

0x1000: 0x7f
0x1001: 0x38
0x1002: 0x39
0x1003: 0x3a
0x1004: 0x4f
0x1005: 0x2b
0x1006: 0x53
0x1007: 0x6e

What is the big endian value of the 4-byte integer at address 0x1004?

  • [ ] (a) 0xe635b2f4
  • [ ] (b) 0x4f2b
  • [ ] (c) 0x3a39
  • [ ] (d) 0x6e53
  • [ ] (e) 0x7f38393a
  • [ ] (f) 0x7f38
  • [ ] (g) 0xf4
  • [ ] (h) 0x83
  • [ ] (i) 0x3a39387f
  • [ ] (j) 0x6e532b4f
  • [ ] (k) 0x7f
  • [ ] (l) 0xe6
  • [ ] (m) 0x3a
  • [x] (n) 0x4f2b536e

Problem 6

Consider a memory unit that is shared between two CPUs: one uses big endian, and the other uses little endian. Assume that the big-endian CPU stores, at address 0x8e497b10, a 4-byte integer with the value 374890817 (or 0x16586141 in hex).

After the operation completes, the little-endian CPU reads a 4-byte integer from address 0x8e497b10. What is the value of the integer read by the little-endian CPU? You may provide your answer in base 10 or in base 16, but if using base 16 you must include the prefix 0x.

0x41615816

Problem 7

Assuming that i is an integer variable that stores a non-negative value, which of the following computes the same value as i % 8 (i.e., , or the remainder when dividing i by 8).

  • [ ] (a) i << 8
  • [ ] (b) i & 8
  • [x] (c) i & 7
  • [ ] (d) i << 3
  • [ ] (e) i >> 8
  • [ ] (f) i & 3
  • [ ] (g) i >> 3

Problem 8

Assuming i is an integer variable whose value is non-negative and evenly divisible by 4. Which of the following computes the same value as i / 4 for all possible values of i?

  • [x] (a) i >> 2
  • [ ] (b) i & 2
  • [ ] (c) i << 2
  • [ ] (d) i & 4
  • [ ] (e) i << 4
  • [ ] (f) i >> 4
  • [ ] (g) i & 3

Problem 9

Consider a computer system that uses big-endian integer representation. Assume that the CPU stores, at address 0x8ab25f28, the 4-byte integer value 0x774f471d.

After the operation completes, the CPU reads, from address 0x8ab25f28, a 2-byte integer. What is the value of the integer read by this operation? Provide your answer in base 16. If the information above is not enough to determine the result, keep the answer blank.

0x774f

Problem 10

What is the value of i in hexadecimal after the following C statements execute?

char b = 0x70;
int i = 0xff & (b << 4);

0x00

Problem 11

What does the following Java program print?

void foo() {
  int a = 0x1266b101;
  a = ((0xf & a) << 28) | (a >>> 4);
  System.out.printf("0x%x\n", a);
}

0x11266b10

Problem 12

Consider the following declaration and initialization of C global variables.

char c;
int i;
  1. Following the execution of this code
i = (char) 0xd9;

What is the value of this variable?

i=0xffffffd9

  1. Following the execution of this code
i = (unsigned char) 0xea;

What is the value of this variable?

i=0xea

  1. Following the execution of this code
i = 0xce2b708a & 0x90000099;

What is the value of this variable?

i=0x80000088

  1. Following the execution of this code
i = (int)0xc4ebadb6 >> 2;

What is the value of this variable?

i=0xf13aeb6d

  1. Following the execution of this code
c = 0x87;
i = (c & (int) 0x90000036) >> 2;

What is the value of this variable?

i=0xe4000001