Consider an 8-bit complier, in which we have an integer of size, say 2-words i.e. 16-bits or 2 bytes. You need to store three integer values which are supposed to be as:
Name Range Size Required -------------------------------------------------- Integer 1 0-3 2bits (00-11 in binary) Integer 2 0-5 3bits (000-101 in binary) Inetger 3 0-7 3bits (000-111 in binary)
In total, we need only 8 bits to store all of these three values. Which means that a single variable of integer datatype has enough memory to accommodate these values. The challenge is to store all of these three variables into a single instance of integer datatype and retrieve it whenever required.
In other words, you need to develop a program which can - at the maximum - use only two instances of integer datatype, first one for the purpose of scanning values from the standard input device say keyboard and the second one to store the data entered by user in the form of three integer variables as discussed above and print them by retrieving it from the second instance in which the data was stored.
This is my program with no input validation. Please review it.
#include <stdio.h>
#include <assert.h>
#ifdef DEBUG
void test();
#endif
#define store(_out, _in, _pos) ((_out) |= ((_in) << (_pos)))
#define ret(_out, _pos, _mask) (((_out) >> (_pos)) & (_mask))
#define firstnum_pos 0
#define secondnum_pos 2
#define thirdnum_pos 5
#define firstnum_mask 0x3
#define secondnum_mask 0x7
#define thirdnum_mask 0x7
int main()
{
unsigned short input = 0, output = 0;
printf ("Enter first number:");
scanf ("%hu", &input);
store(output, input, firstnum_pos);
printf ("Enter second number:");
scanf ("%hu", &input);
store(output, input, secondnum_pos);
printf ("Enter third number:");
scanf ("%hu", &input);
store(output, input, thirdnum_pos);
printf ("first number: %d", ret(output, firstnum_pos, firstnum_mask));
printf ("second number: %d", ret(output, secondnum_pos, secondnum_mask));
printf ("third number: %d", ret(output, thirdnum_pos, thirdnum_mask));
#ifdef DEBUG
test();
#endif
return 0;
}
#ifdef DEBUG
void test()
{
int i,j,k;
short out;
for (i=0; i<=3; i++)
for (j=0; j<=5; j++)
for (k=0; k<7; k++)
{
out = 0;
store(out, i, firstnum_pos);
store(out, j, secondnum_pos);
store(out, k, thirdnum_pos);
assert (i == ret(out, firstnum_pos, firstnum_mask));
assert (j == ret(out, secondnum_pos, secondnum_mask));
assert (k == ret(out, thirdnum_pos, thirdnum_mask));
}
printf ("Test completed\n");
}
#endif