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
-
\$\begingroup\$ Checked this code in both little endian and big endian machines. \$\endgroup\$user138645– user1386452012年06月07日 06:36:55 +00:00Commented Jun 7, 2012 at 6:36
2 Answers 2
Some ideas:
I'd try encapsulating the position and mask parameters to one parameter. Maybe a struct would be fine for this. It is easy to mix up the parameters of
ret
, for example, somebody could call it accidentally with`ret(output, secondnum_pos, firstnum_mask)`
or with
`ret(output, firstnum_mask, firstnum_pos)`.
(Note the parameter order.)
Consider writing tests which store the
i
,j
,k
values in different order. Currently they do not detect if storing at thefirstnum_pos
overrides the second or the third values.Consider making copies of
i
,j
andk
for theassert
calls before you call the marcos. If a macro accidentally changes values ofi
,j
ork
the assert will not detect it.A unit testing framework would be better for the testing: Unit Testing C Code
Your code only works if the bits are not set. If you set the value multiple times, it will not work correctly.
Explore related questions
See similar questions with these tags.