# Assignment 1 – The C Preprocessor and Bit Operations solved

\$35.00

## Description

5/5 - (1 vote)

Problem 1.1 Circular permutation of three variables (2 points)
Presence assignment, due by 18:30 h today
Write a macro and a program for the circular permutation of the contents of three variables having the same data type (i.e., the content of the first is put into the second, the content of the second
into the third and the content of the third into the first). The macro should have four parameters:
the three variables and their corresponding data type.
Your program should read three integers and three doubles from the standard input. You should
print on the standard output the contents of the six variables after the permutation (doubles with
5 after floating point precision).
You can assume that the input will be valid.
Testcase 1.1: input
1
2
3
3.45
5.677
8.98273
Testcase 1.1: output
After the permutation:
3
1
2
8.98273
3.45000
5.67700
Problem 1.2 Determine the third least significant bit (1 point)
Presence assignment, due by 18:30 h today
Write a macro and a program for determining the third least significant bit (the third bit from the
right in the binary representation) of an unsigned char read from the standard input.
Your program should read an unsigned char from the standard input and print the decimal representation of the unsigned char as well as its third least significant bit (which is either 1 or 0)
on the standard output using only bitwise operators and without explicitly converting to binary
representation.
You can assume that the input will be valid.
Testcase 1.2: input
F
Testcase 1.2: output
The decimal representation is: 70
The third least significant bit is: 1
Problem 1.3 Determine the value of an expression (2 points)
Write multiple macros and a program for determining the value of the following expression depending on three variables a, b, and c calculated as
expr(a, b, c) = sum(a, b, c) + max(a, b, c)
min(a, b, c)
.
For example if 3, 10, 2 is the input, the value of the expression is
expr(3, 10, 2) = sum(3, 10, 2) + max(3, 10, 2)
min(3, 10, 2) =
15 + 10
2
=
25
2
= 12.5.
Your program should read three integers from the standard input. For calculating the expression
for these values only macros should be used. The result should be printed on the standard output
with a floating point precision of 6.
You can assume that the input will be valid.
Testcase 1.3: input
3
10
2
Testcase 1.3: output
The value of the expression is: 12.500000
Problem 1.4 Conditional compilation for showing intermediate results (2 points)
Write a program which computes the product of two n × n integer matrices and uses conditional
compilation for showing/not showing intermediate results (products of the corresponding components). The product of two n × n matrices A = (Aij) and B = (Bjk) with i, j, k = 1, . . . n is
calculated as
Cik =
Xn
j=1
Aij · Bjk, with i = 1, . . . , n and k = 1, . . . , n.
For example the product of the matrices A =

1 2
3 4
and B =

1 2
3 4
is
C =

1 · 1 + 2 · 3 1 · 2 + 2 · 4
3 · 1 + 4 · 3 3 · 2 + 4 · 4

=

1 + 6 2 + 8
3 + 12 6 + 16
=

7 10
15 22
.
The intermediate results which are to be shown or not are 1, 6, 2, 8, 3, 12, 6, and 16.
Your program should read from the standard input the dimension of the matrix (in the previous
example 2) along with the components of two integer matrices. The output consists of the intermediate results and the value of the product of the two matrices if the directive INTERMEDIATE
is defined. If INTERMEDIATE is not defined then only the product of the two matrices should
be printed on the standard output.
You can assume that the input will be valid.
Testcase 1.4: input
2
1
2
3
4
1
2
3
4
Testcase 1.4: output
The intermediate product values are:
1
6
2
8
3
12
6
16
The product of the matrices is:
7 10
15 22
Problem 1.5 Binary representation backwards (1 point)
Write a program using bit masks and bitwise operators for printing the binary representation of
an unsigned int backwards. For example the binary representation of the unsigned int 12345 on
16 bits is 0011000000111001. Therefore, the backwards binary representation is 10011100000011.
Your program should read an unsigned int from the standard input and print on the standard output the backwards binary representation of the read integer without leading zeros. You should
not store the bits in an array.
You can assume that the input will be valid.
Testcase 1.5: input
12345
Testcase 1.5: output
The backwards binary representation is: 10011100000011
Problem 1.6 Binary representation (2 points)
Write a program using bit masks and bitwise operators for printing the binary representation of
an unsigned int on 16 bits without storing the bits in an array. For example the binary representation of the unsigned integer number 12345 on 16 bits is 0011000000111001.
Your program should read an unsigned int from the standard input and print on the standard
output the binary representation of the integer number with leading zeros.
You can assume that the input will be valid.
Testcase 1.6: input
12345
Testcase 1.6: output
The binary representation is: 0011000000111001
Problem 1.7 setswitchbits() (2 points)
Write a program for setting one bit to 1 and switching another bit of an unsigned int. The function
setswitchbits should have three parameters: the unsigned int to be changed and the two
bits. The first bit is to be set to 1 and the other bit is to be switched. For example the binary
representation on 16 bits of the unsigned int 12345 is 0011000000111001. If setswitchbits()
with bits 6 (the 7
th bit from the right) and 12 (the 13th bit from the right) is called then the output
on the standard output should be 0010000001111001.
You can assume that the input will be valid.
Testcase 1.7: input
12345
6
12
Testcase 1.7: output
The binary representation is: 0011000000111001
After setting and switching: 0010000001111001
• Your source code should be properly indented and compile with gcc without any warnings (You
can use gcc -Wall -o program program.c). Insert suitable comments (not on every line . . . ) to
• Please name the programs according to the suggested filenames (they should match the description
of the problem) in Grader. Otherwise you might have problems with the inclusion of header files.
Each program must include a comment on the top like the following:
/*
JTSK-320112
a1 p1.c
Firstname Lastname
myemail@jacobs-university.de
*/