473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Portable assignment

I am reviewing this function:

unsigned long function(unsigned long *first, unsigned long *last)
{
unsigned long diff;

if ( (*last) < (*first) /* rollover occured */
{
period = (0xFFFFFFFF - (*first)) + (*last) + 1;
}
else /* no rollover */
{
period = (*last) - (*first);
}

return period;
}
The statement
period = (0xFFFFFFFF - (*first)) + (*last) + 1;
assumes a long is 32 bits wide. I noticed in K&R2 they talked about a
portable way of defining a constant - without assuming a particular width. I
came up with these:

Suggestion 1:
period = (~0UL - (*first)) + (*last) + 1;

Suggestion 2:
period = (ULONG_MAX - (*first)) + (*last) + 1;

The point of Suggestion 1 is that it sets all bits in an unsigned long,
irrespective of the width it happens to be. Suggestion 2 uses the symbolic
constant ULONG_MAX (the maximum value for an object of type unsigned long
int).

My questions are these:
(i) Am I right to try and make this non-width specific?
(ii) Do my suggestions so do?

Finally, as an adjunct, if I wanted to set a value, say 0xFFD1 in an
unsigned int that happens to be 16 bits wide at the moment, would I be
better off doing something like this:

ui = ~0x2EU;

so later when it's 24 or 32 bits, or whatever, all bits except 1, 2, 3, and
5 are still set?

--
Martin
http://martinobrien.co.uk/

Nov 14 '05 #1
4 1041
Martin <martin.o_brien@[no-spam]which.net> wrote:

My questions are these:
(i) Am I right to try and make this non-width specific?
Yes.
(ii) Do my suggestions so do?
Yes. However, you've overlooked a much better solution to the problem. Due
to the properties of unsigned arithmetic in C:
period = (0xFFFFFFFF - (*first)) + (*last) + 1;
computes exactly the same value (assuming 32-bit longs) as:
period = (*last) - (*first);


So there's no need for any constant. In fact, there's no need for the
function itself since the simple, obvious expression works correctly,
even in the case of (a single) wrap around!

-Larry Jones

You should see me when I lose in real life! -- Calvin
Nov 14 '05 #2
Larry Jones already answered the main question (and noted one
of the very useful properties of unsigned arithmetic: ordinary
subtraction always gives the desired result).

In article <news:oW******************@fe08.usenetserver.com >
Martin <martin.o_brien@[no-spam]which.net> writes:
Finally, as an adjunct, if I wanted to set a value, say 0xFFD1 in an
unsigned int that happens to be 16 bits wide at the moment, would I be
better off doing something like this:

ui = ~0x2EU;

so later when it's 24 or 32 bits, or whatever, all bits except 1, 2, 3, and
5 are still set?


It is certainly a good idea to consider this option. The question
then becomes whether you want to set any "new" (higher-order) bits
that appear in the unsigned type when it has a bigger U<type>_MAX
value. This depends on the "intended purpose" of the unsigned int,
rather than its actual representation in a 16-or-more-bit unit.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3
In article <oW******************@fe08.usenetserver.com>,
"Martin" <martin.o_brien@[no-spam]which.net> wrote:
I am reviewing this function:

unsigned long function(unsigned long *first, unsigned long *last)
{
unsigned long diff;

if ( (*last) < (*first) /* rollover occured */
{
period = (0xFFFFFFFF - (*first)) + (*last) + 1;
}
else /* no rollover */
{
period = (*last) - (*first);
}

return period;
}


Very strange. If unsigned long is 32 bit, then this will always return
the same value as

return *last - *first;

If unsigned long is more than 32 bit, then the result is very likely not
what the programmer intended. You could just change this to

unsigned long function (unsigned long *first, unsigned long *last)
{
return *last - *first;
}
Nov 14 '05 #4
OK, my thanks to Lawrence and Chris for your responses. Much appreciated.

--
Martin
http://martinobrien.co.uk/

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
13
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows),...
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
32
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
9
by: Martin Wells | last post by:
I'm doing an embedded systems project and I'm programming it as fully- portable C89 (except of course for setting the pin values). I need to put delays in the program, in the vicinity of 250 ms....
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.