C programming help required ....

Roger Oberholtzer roger
Fri Aug 26 10:40:53 PDT 2005


On Fri, 2005-08-26 at 14:59, Ben Duncan wrote:
> Ok, the number will always greater than 1001 and less than 999,999
> Only ONE attribute and ONE multi value per integer will be stored.
> Each integer represents a single attribute/multi value pair.
> 
> 
> The break down is thus [ AAA,MMM ]
>    where AAA = attribute number and
>          MMM = Multi value number.
> 
> SO the integer number 7001 would represent
>      Attribute 7
>      First multi value in attribute 7
> 
> conversely integer number 7010 would represent
>      Attribute 7
>      the tenth multi value in attribute 7
> 

The easiest way to do this is to consider the values to be hex, not
decimal. Otherwise you will be stuck doing arithmetic! Whenever you do
this sort of bit fiddling, this will make your life easiest. Is this a
number a person would enter? Or is it internal? There are really many
ways to do this, but they follow the same basic method. 

/* Do these defines once in some include file */

#define ATTRIBUTE_MASK		0x00FFF000
#define MVN_MASK		0x00000FFF

#define	GET_ATTRIBUTE(x)		(((x) & ATTRIBUTE_MASK) >> 3)
#define GET_MULTI_VALUE_NUMBER(x)	((x) & MVN_MASK)

/* Here is the code that uses this: */

int sample_value = 0x7001;	/* The 0x IS REQUIRED */

printf("value %x has attribute %d and mvn %d\n",
	value,
	GET_ATTRIBUTE(value),
	GET_MULTI_VALUE_NUMBER(value));

This can be made better - depending on your real use. If someone will be
entering this value in some menu, let them enter 7001, but make your
program read it as hex (scanf(%x)). The user need not know this is how
you use it.

The GET_xxx macros should have equivalent SET_xxx macros. But those can
have macro expansion side effects. So I would do them as inline
functions. Still fast - but no nasty side-effects.

Is this the sort of thing you mean?

> 
> Thanks ...
> 
> 
> Roger Oberholtzer wrote:
> > On Fri, 2005-08-26 at 03:49, Ben Duncan wrote:
> 
> 
> > 
> > These are very easy in C. 
> > 
> > Can you be more clear about what 7001 means? Is all of that number only
> > indicating attribute 7? Or only part of it? What all should be stored in
> > the 32 bits? How many attributes are to be supported in this integer?
> <SNIP>
> 
> 
> > 
> > 
> > 
> >>int ATTRIBUTE, SUBVAL ;
> >>
> >>SUBVAL = KEYLIST[5] (and'ed or "or"d or shifted ?!?!)
> >>ATTRIBUTE = KEYLIST[5] (and'ed or "or"d or shifted ?!?!)
> >>
> >>and now SUBVAL equals 1 and ATTRIBUTE equal 7 ...
> >>
+????????????????????????????+???????????????????????????????+
? Roger Oberholtzer          ?   E-mail: roger at opq.se        ?
? OPQ Systems AB             ?      WWW: http://www.opq.se/  ?
? Kapellgr?nd 7              ?                               ?
? P. O. Box 4205             ?    Phone: Int + 46 8   314223 ?
? 102 65 Stockholm           ?   Mobile: Int + 46 733 621657 ?
? Sweden                     ?      Fax: Int + 46 8   314223 ?
+????????????????????????????+???????????????????????????????+



More information about the Linux-users mailing list