C programming help required ....
Roger Oberholtzer
roger
Sun Aug 28 04:48:15 PDT 2005
On Sat, 2005-08-27 at 10:43 -0700, Thomas Condon wrote:
> Roger Oberholtzer wrote:
>
> >On Fri, 2005-08-26 at 11:50 -0500, Ben Duncan wrote:
> >
> >
> >>It would be something they entered one time while setting up the file.
> >>Probably will be done thru some sort of Data Entry routine so I can control
> >>it.
> >>
> >>
> >
> >That should present no trouble. I bet your numbers were already
> >conceptually compatible with the approach I suggested. It is just that
> >you should use the scanf '%x' to read then from a user and '%x' to print
> >them in ASCII. The actual integer is just used as is. Taking this
> >approach allows you to use bit fields (mask/shift) instead of odd math
> >to access the components in your numbers.
> >
> >
> >
> The "odd" mathematical instructions to perform these two functions are:
>
> Upper = (int)(value / 1000 )
> Lower = value % 1000
In this case, true. But any time I want to encode a number to contain
multiple things, I automatically take the route I outlined. Old habits
and all. The only valid reasons I could have for continuing to suggest
this is that it it such a common technique that any other programmer
looking at the code would probably immediately recognize it. The other
reason I would give is that the mask operations are very very very much
faster. I code DPSs that far too ofter lack the cycles I need. I know
that CPU speed these days is often taken for granted. But once again,
old habits...
>
> That really doesn't seem all that difficult to me.
>
> Dividing by 1000 essentially shifts right (but shifting only works
> evenly for powers of 2). Casting the result to an int should not be
> necessary, but it has been my experience that I only get myself in
> trouble when I 'expect' the compiler designer to have done things right.
>
> The "%" operator is "modulus", which is the remainder when the first
> number (value) is divided by the second (1000). Modulus essentially
> masks the left half of the number, but bit masking only works for powers
> of 2, also.
>
> No need to deal with hex at all.
Aside from formatting the value when printing it (change your %d to %x)
and reading from user input (change your %d to %x) it is all internal to
the program and of no concern at all to the user. The hex think is more
a way to make clear in the C source how the number is encoded. When
actually using the number in the program, it is an integer plain and
simple.
C strikes again. More than one way of doing anything.
>
> Hope this helps.
>
>
> Tom Condon
>
>
> _______________________________________________
> Linux-users mailing list ( Linux-users at linux-sxs.org )
> Unsub/Password/Etc: http://mail.linux-sxs.org/cgi-bin/mailman/listinfo/linux-users
>
> Need to chat further on this subject? Check out #linux-users on irc.linux-sxs.org !
>
More information about the Linux-users
mailing list