C programming help required ....
Thomas Condon
tomc
Sat Aug 27 12:16:13 PDT 2005
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
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.
Hope this helps.
Tom Condon
More information about the Linux-users
mailing list