CISC105 - General Computer Science
Homework #4
(solutions)
Pointers and Operations on Bits
1. (5) What is the address operator?
&
2. (5) What is the indirection operator?
*
3. (5) What is the value of x?
int counter = 25;
int *a_pointer;
a_pointer = &counter;
x = *a_pointer;
x = 25
4. (5) Using a pointer, set the month of today's date to 12.
struct date
{
int month;
int day;
int year;
};
struct date todays_date;
struct date *p1;
p1 = &todays_date;
p1->month = 12; or (*p1).month = 12;
5. (5) Code the C statements to link the following lists together.
struct item
{
int value;
struct item *next;
};
struct item list1, list2, list3;
list1.next = &list2;
list2.next = &list3;
6. (5) Code the C statements to link 'list2a' in between 'list2' and 'list3'
(use code from Problem #5).
struct item list2a;
list2a.next = list2.next;
list2.next = &list2a;
7. (5) Code the C statement that marks 'list3' as the end of the linked list
(use code from Problem #5).
list3.next = 0;
8. (5) Using the following code, set x equal to the fourth element of the array
values (use a pointer).
int x, values[], *values_pointer;
values_pointer = values;
x = *(values_pointer+3);
9. (5) Using the following code, what is the value of x?
static char string1[] = "This is a character string";
char c, x, *c_pointer;
c_pointer = string1;
x = *(c_pointer);
x = 'T'
10. (5) Using the code from Problem #9, what are the values of a and b?
char a, b;
c_pointer = &string1[12];
a = *(c_pointer++);
b = *(--c_pointer);
a = 'a'
b = 'a'
11. (5) Given the following function, declare a pointer to a function that returns
an int and assign the function to it.
int lookup ();
int (*func_ptr) (); or int *func_ptr = lookup;
func_ptr = lookup;
12. (5) Convert -25 (decimal) to binary using two's compliment (use an 8 bit word).
Add +1 -24
Absolute Value 24
Change to Binary 00011000
Compliment the Bits 11100111
13. (5) Perform the following bitwise operation.
w1 0 1 0 0 1 1 1 0
w2 1 1 0 1 0 1 0 1
w1 & w2 0 1 0 0 0 1 0 0
14. (5) Perform the following bitwise operation.
w1 0 1 0 0 1 1 1 0
w2 1 1 0 1 0 1 0 1
w1 | w2 1 1 0 1 1 1 1 1
15. (5) Perform the following bitwise operation.
w1 0 1 0 0 1 1 1 0
w2 1 1 0 1 0 1 0 1
w1 ^ w2 1 0 0 1 1 0 1 1
16. (5) Perform the following bitwise operation.
w1 0 1 0 0 1 1 1 0
~w1 1 0 1 1 0 0 0 1
17. (5) Perform the following bitwise operation.
w1 0 1 0 0 1 1 1 0
w1 << 3 0 1 1 1 0 0 0 0
18. (5) Perform the following bitwise operation.
w1 0 1 0 0 1 1 1 0
w1 >> 2 0 0 0 1 0 0 1 1
19. (5) Perform the following bitwise operation.
w1 1 0 0 1 1 1
w2 0 1 0 1 1 0 0 1
w1 & w2 0 1 0 0 0 0 0 1
20. (5) Define a structure of packed data where the first two bits are flags, the next
two bits are a type field, and the remaining four bits are an address field.
struct packed_date
{
unsigned int flag1:1;
unsigned int flag2:1;
unsigned int type:2;
unsigned int address:4;
};