Study Guide 3
Quizzes are closed book. You can bring one written cheat sheet. 30 minutes in class.
Topics:
Everything from Study Guide 2, plus
-
Data type formats: signed and unsigned integers, float, double
-
Bitwise operators
-
Binary arithmetic
-
Memory layouts of arrays and structs, byte alignment
-
Reading and writing binary files
Write C programs to check your answers. Or you can ask a TA or instructor to give feedback on your responses.
Additional Practice questions
1) Convert the following binary numbers to hexadecimal and decimal. Try without a calculator (but use a calculator to check your results)
-
0110 1010
-
1110 0011
-
0110 1110
2) Convert the following hexadecimal numbers to binary and decimal. Try without a calculator (but use a calculator to check your results)
-
0xF5
-
0x03
-
0x2D
3) Convert the following decimal numbers to hexadecimal and binary. Try without a calculator (but use a calculator to check your results)
-
101
-
54
-
25
4) Compute the results of the following decimal expressions using 4-bit unsigned integers. Express your final answer in hexadecimal. Show your work.
Expression |
Result |
3 << 8 |
|
2 & 7 |
|
2 + 7 |
|
9 + 7 |
|
11-9 |
|
5) Compute the results of the following decimal expressions using 4-bit signed integers. Express your final answer in hexadecimal. Show your answer.
Expression |
Result |
-6 >> 3 |
|
2 ^ 7 |
|
2 + 7 |
|
5 - 7 |
|
-4-1 |
|
6) Convert the following hexadecimal number 0x 3f c0 00 00
-
to a floating point number
-
to an unsigned integer
-
to a signed integer
-
to an 32-bit RGBA color, e.g. consider each byte as a value for red, green, blue, and alpha
6) Convert the following hexadecimal number 0x 3f 6a 42
-
to an unsigned integer
-
to a signed integer
-
to an 24-bit RGB color, e.g. consider each byte as a value for red, green, and blue
-
to an ASCII character string
8) Consider the following struct. Draw the contents and layout of a variable of this struct type, assuming the variable is placed in memory starting at address 0xfffffe40.
struct foo {
float x;
unsigned char c[3];
struct foo* v;
};
9) Sheldon uses the following program to write an unsigned integer to a file in binary format.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE* fp = fopen("test.bin", "wb");
const char* message = "a-ok";
fwrite(message, sizeof(unsigned char), 5, fp);
unsigned int v = 7;
fwrite(&v, sizeof(unsigned int), 1, fp);
fclose(fp);
}
When they inspect the file using hexedit, they see the following result. Does it make sense? Why or why not?
00000000 61 2D 6F 6B 00 07 00 00 00 a-ok.....
00000018
9) Ava implemented the following program to keep track of her finances. However, it’s not working properly. What is going on and how can she fix it?
#include <stdio.h>
int main() {
unsigned int balance = 0;
FILE* infile = fopen("finances.txt", "r");
fscanf(infile, " %u", &balance);
fclose(infile);
printf("Your balance: $%u\n", balance);
printf("What would you like to do? (d = deposit,w = withdraw)\n");
char choice = fgetc(stdin);
if (choice != 'd' && choice != 'w') {
printf("Invalid choice! Quitting....\n");
return 1;
}
int amount = 0;
printf("Enter an amount: ");
scanf(" %d", &amount);
if (amount < 0) {
printf("Invalid amount! Quitting...\n");
return 1;
}
if (choice == 'd') {
balance += amount;
} else if (choice == 'w') {
balance -= amount;
}
FILE* outfile = fopen("finances.txt", "w");
fprintf(outfile, "%u", balance);
fclose(outfile);
return 0;
}
$ ./finance
Your balance: $50
What would you like to do? (d = deposit,w = withdraw)
d
Enter an amount: 5
$ ./finance
Your balance: $55
What would you like to do? (d = deposit,w = withdraw)
w
Enter an amount: 40
$ ./finance
Your balance: $15
What would you like to do? (d = deposit,w = withdraw)
w
Enter an amount: 20
$ ./finance
Your balance: $4294967291
What would you like to do? (d = deposit,w = withdraw)
^C
10) Consider the following program
1 #include <stdio.h>
2
3 int main() {
4 unsigned char a = 0x7E;
5 unsigned char leftMask = 0xC0;
6 unsigned char rightMask = 0x03;
7 unsigned char left = (leftMask & a) ;
8 unsigned char right = (rightMask & a) ;
9 unsigned char leftShift = left >> 4;
10 unsigned char rightShift = right << 4;
11 unsigned flipped = leftShift | rightShift;
12
-
What is the value of
a
in binary? -
What is the value of
flipped
? Show your work to give a rationale for your answer.
11) Consider the following file permissions.
-rw-r--r-- 1 alinen alinen 191 Sep 10 12:07 greeting.c
-rwxr-xr-x 1 alinen alinen 15960 Sep 3 12:33 hello
-rw-r--r-- 1 alinen alinen 73 Sep 3 12:26 hello.c
-
What octal number corresponds to the file permissions for
hello.c
? -
What octal number corresponds to the file permissions for
hello
? -
Suppose we want to change the permissions for
hello
so that groups have read/write access. What octal number would correspond to the new permission?
12) Suppose we have a 4-bit bit field that stores whether an animal can fly, swim, or walk, defined with the following masks
#define NONE 0x0
#define FLY 0x1
#define SWIM 0x2
#define WALK 0x4
-
Suppose an animal’s bitfield contains the value 0x3. What capabilities does it have?
-
What value would the bitfield have for an otter, which can swim and walk?
-
What value would the bitfield have for a duck, which can fly, swim and walk?
-
What value would the bitfield have for a cow, which can only walk?
Programming
1) Write a program, lswizzle.c
that shifts all the bits in an unsigned integer to the left, wrapping the most significant bit into the least significant bit. For example, if the integer has binary representation 0b 1010 0110 0000 0000
, the result should be 0b 0100 1100 000 0001
.
2) Write a program, odd.c
that uses xor to identify the single element that appears an odd number of times in the following list: [4, 6, 8, 8, 6, 2, 3, 4, 3].
3) Write a program, flag.c
, that allows the user to set or unset a specific bit in an unsigned integer.
$ ./flag
usage: flag
$ ./flag 101 4 1
Your original number: 0x65 (101)
Your new number: 0x75 (117)
$ ./flag 101 3 1
Your original number: 0x65 (101)
Your new number: 0x6d (109)