### Study Guide 3

Code Jams are open book. 40 minutes in lab.

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

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

 Expression Result 3 << 8 2 & 7 2 + 7 9 + 7 11-9

 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 RGBA color, e.g. consider each byte as a value for red, green, blue, and alpha

• 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;
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("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
What would you like to do? (d = deposit,w = withdraw)
d
Enter an amount: 5
\$ ./finance
What would you like to do? (d = deposit,w = withdraw)
w
Enter an amount: 40
\$ ./finance
What would you like to do? (d = deposit,w = withdraw)
w
Enter an amount: 20
\$ ./finance
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.

### 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