Name:
cs202 login:
Quiz 6 - Bit Operations and Program Memory
1) Program Memory Organization
Given a complete C program, tell where each variable is stored in program
memory. Example:
Given:
#include <stdio.h>
char c = 'a';
int main(int argc, char *argv[])
{
int x = 20;
int a[20];
int *b = malloc(10 * sizeof(int));
}
Questions:
1.1) Where is x stored?
1.2) Where is argc stored?
1.3) Where is b stored?
1.4) Where is b[2] stored?
1.5) Where is a[1] stored?
etc.
Will be worth 10 points on the quiz.
2) Dynamic memory allocation
2.1) Give the line of C code that allocates enough space for 15 char type
variables to a pointer called b.
2.2) Give the line of C code that changes the amount of space allocated to b
to 20 char type variables.
etc.
3) Operators (for pointers and bitwise operations)
Describe the function(s) of the following operators given the context:
3.1) *a
3.2) char *a
3.3) a || b
3.4) b << 4
3.5) &x
etc.
4) Bitwise Operations
Assume we have the following functions defined:
unsigned char set(unsigned char c, int i);
unsigned char get(unsigned char c, int i);
unsigned char clear(unsigned char c, int i);
unsigned char toggle(unsigned char c, int i);
Evaluate the following operations (show your work):
4.1) set(52, 3)
4.2) get(23, 0)
4.4) clear(963, 1)
etc.
5) Complete C Program
Write a complete C program for one of the following problems:
- Read an integer n. Allocate enough memory for n int types in an array,
then read n integers into the array.
- Write a program that keeps reading integers into an array, growing the
size of the array as needed.
- Write a C program that reads an unsigned int from stdin, count the
number of ones and zeros in its binary representation.
- Write a C program that reads an unsigned int from stdin, determine if
the binary representation of the integer is a palindrome.
NOTE: A few for loop / control flow questions may be sprinkled throughout
- Write the code for a for loop that prints every element in the following
array in reverse order:
int a[5];
- Write the code for an if statement that checks whether the char type
varible c is an uppercase letter or lowercase letter or neither.