CMPSC 461: Programming Language Concepts Assignment 6 Solution
Problem 1 [6pt] Consider a 32-bits binary as follows: 0100 0000 0111 1100 0000 0000 0000 0000
What will be the value of it if it is interpreted as
• 32-bit integer?
• single-precision floating number? • 4 characters in ASCII code?
Solution:
• integer: 1081868288
• floating point number: (−1)0 × 2128−127 × 1.96875 = 3.937500 • characters: @ I NULL NULL
Problem 2 [8pt] Consider the following C declaration:
struct S1 {int a; double b;}; union S2 {float b; char a;}; union U {
struct S1 s1;
union S2 s2; } u;
Assume the machine has 1-byte characters, 4-byte integers, 8-byte floating numbers, and 16-byte double- precision floating numbers. Assume the compiler does not reorder the fields, and it leaves no holes in the memory layout. How many bytes does u occupy? If the memory address of u starts from 1000, what are the start addresses of u.s1.b and u.s2.b? Justify your answer by showing the memory layout of u.
Solution:
u occupies max(4+16, max(8,1))=20 bytes. The start address of u.s1.b is 1004; the start address of u.s2.b is 1000.
Problem 3 [8pt] RSA is an algorithm used by modern computers to encrypt and decrypt messages. The core of RSA is the computation of (pk mod n) where k is confidential (i.e., with k, an attacker can derive the private key). For efficiency, some implementations of RSA use a window-based modular exponentiation, which creates a two-dimensional array, say A, so that A[i][j] stores the j-th byte of (pi mod n), where 0 ≤ i ≤ 23 − 1, assuming a window size of 3. Note that 384 bytes are typically needed for each A[i] since p, k, n are large numbers. The algorithm works as follows:
for (i=1; i