patterncMinor
Storing three integer values
Viewed 0 times
threevaluesstoringinteger
Problem
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:
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
#include
#ifdef DEBUG
void test();
#endif
#define store(_out, _in, _pos) ((_out) |= ((_in) > (_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, secon
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
#include
#ifdef DEBUG
void test();
#endif
#define store(_out, _in, _pos) ((_out) |= ((_in) > (_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, secon
Solution
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
or with
(Note the parameter order.)
-
Consider writing tests which store the
-
Consider making copies of
-
A unit testing framework would be better for the testing: Unit Testing C Code
-
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 the firstnum_pos overrides the second or the third values.-
Consider making copies of
i, j and k for the assert calls before you call the marcos. If a macro accidentally changes values of i, j or k the assert will not detect it.-
A unit testing framework would be better for the testing: Unit Testing C Code
Code Snippets
`ret(output, secondnum_pos, firstnum_mask)``ret(output, firstnum_mask, firstnum_pos)`.Context
StackExchange Code Review Q#12332, answer score: 2
Revisions (0)
No revisions yet.