Here is a short C program with two macros, MIN and MAX, together with examples o
ID: 3568079 • Letter: H
Question
Here is a short C program with two macros, MIN and MAX, together with
examples of how each would be used. The implementation is sound.
(In the calls, note that the parameterized macros look as if they were functions.)
By convention, macros are capitalized, but this is not a requirement. The MIN and MAX
macros are "parameterized": each takes two signed integer values. MIN returns the smaller
of the two; MAX returns the larger of the two.
#include <stdio.h>
#define MIN(x, y) (y) ^ ((x ^ y) & -(x < y))
#define MAX(x, y) (x) ^ ((x ^ y) & -(x < y))
int main() {
printf("%i ", MIN(8, -4)); /* -4 */
printf("%i ", MIN(44 - 64, -18 + -88)); /* -106 */
printf("%i ", MAX(44 - 64, -18 + -88)); /* -20 */
return 0;
}
The macros use various bit-level operations. Show, with an example in binary, how the
macros work when the two arguments differ. In particular, explain at the bit-level how MIN and MAX
achieve their different results. (You need not use the values shown above, but can pick your own.)
Finally, explain what happens with the two arguments to MIN or MAX are the same, e.g., MIN(2, 2).
You can explain either MIN or MAX, as one explanation basically covers both. Your answer should have
two parts:
-- An explanation about why these macros work in general, that is, for any integer values.
-- An example in binary that illustrates exactly how they work
Explanation / Answer
#include #include #include int main( void ) { int i = 0; int a[11]; int min = 0; int max = 0; srandom( (unsigned) time(NULL) ); a[0]=random( ) % 100 ; min = a[0]; for (i=1;i max) { max = a[i]; } if (a[i]Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.