# Assignment 1 – The C Preprocessor and Bit Operations solution

\$24.99

Original Work ?

## Description

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 havingthesamedatatype(i.e.,thecontentoftheﬁrstisputintothesecond,thecontentofthesecond intothethirdandthecontentofthethirdintotheﬁrst). Themacroshouldhavefourparameters: the three variables and their corresponding data type. Yourprogramshouldreadthreeintegersandthreedoublesfromthestandardinput. Youshould printonthestandardoutputthecontentsofthesixvariablesafterthepermutation(doubleswith 5 after ﬂoating 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 signiﬁcant bit (1 point) Presence assignment, due by 18:30 h today Writeamacroandaprogramfordeterminingthethirdleastsigniﬁcantbit(thethirdbitfromthe 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 signiﬁcant 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.
Yourprogramshouldreadthreeintegersfromthestandardinput. Forcalculatingtheexpression forthesevaluesonlymacrosshouldbeused. Theresultshouldbeprintedonthestandardoutput with a ﬂoating 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) Writeaprogramwhichcomputestheproductoftwo n×n integermatricesandusesconditional compilation forshowing/not showing intermediateresults (products ofthe corresponding components). The product of two n × n matrices A = (Aij) and B = (Bjk) with i,j,k = 1,…n is calculated as Cik = n X 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 intermediateresultsandthevalueoftheproductofthetwomatricesifthedirectiveINTERMEDIATE is deﬁned. If INTERMEDIATE is not deﬁned 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. Yourprogramshouldreadanunsignedintfromthestandardinputandprintonthestandardoutput 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) Writeaprogramforsettingonebitto1andswitchinganotherbitofanunsignedint. Thefunction setswitchbits should have three parameters: the unsigned int to be changed and the two bits. The ﬁrst 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 7th 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
How to submit your solutions • Your source code should be properly indented and compile with gcc without any warnings (You canuse gcc -Wall -o program program.c). Insertsuitablecomments(notoneveryline…) to explain what your program does. • Please name the programs according to the suggested ﬁlenames (they should match the description of the problem) in Grader. Otherwise you might have problems with the inclusion of header ﬁles. Each program must include a comment on the top like the following: /* JTSK-320112 a1 p1.c Firstname Lastname myemail@jacobs-university.de */
• You have to submit your solutions via Grader at https://cantaloupe.eecs.jacobs-university.de. If there are problems (but only then) you can submit the programs by sending mail to k.lipskoch@jacobs-university.de with a subject line that begins with JTSK-320112. It is important that you do begin your subject with the coursenumber, otherwise I might have problems to identify your submission. • Pleasenote,thatafterthedeadlineitwillnotbepossibletosubmitanysolutions. Itisuselesstosend late solutions by mail, because they will not be accepted. This assignment is due by Tuesday, February 13th, 10:00 h.