473,890 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

efficiency concern: when to really use unsigned ints and when not to

Hello,

Often I happen to be dealing with nonnegative integers and since I know I won't
need negative numbers here I declare them as unsigned simply to make the program
somewhat clearer. Effectively, though, signed integers would often work too since
unless I am doing some modular arithmetic modulo the word length then I almost
never need to use the high bit since the integers I deal with are usually not
that large, and I would assume this is true of most programs in general.

So, the question is, when you know an integer is not going to be negative, is that
good enough reason to declare it as unsigned, or does doing so somewhat slow down
the computer (e.g. are signed addition, subtraction, etc... somewhat faster and
why would that be so?)?

Thanks,

Neil
Nov 14 '05
49 5933
Neil Zanella wrote:
.... snip ...
for (;;) { /* stuff */ if (n-- == 0) break; }


Much clearer is:

while (n--) { /* stuff */ }

Department of simplistic clarification.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #31
CBFalconer wrote:

Neil Zanella wrote:

... snip ...

for (;;) { /* stuff */ if (n-- == 0) break; }


Much clearer is:

while (n--) { /* stuff */ }

Department of simplistic clarification.


Your while loop has different semantics from that particular for loop.

I'm not sure whether I prefer
do { /* stuff */ } while (n-- != 0);
or
do { /* stuff */ } while (n--);

I think that the meaning of while (n--)
is just as clear as that of while (n-- != 0)

--
pete
Nov 14 '05 #32
pete wrote:
CBFalconer wrote:
Neil Zanella wrote:

... snip ...

for (;;) { /* stuff */ if (n-- == 0) break; }


Much clearer is:
while (n--) { /* stuff */ }


Your while loop has different semantics from that particular for loop.

I'm not sure whether I prefer
do { /* stuff */ } while (n-- != 0);
or
do { /* stuff */ } while (n--);


Well caught. However the point is that for loops are not best
suited in many places. Also that break is almost as confusing as
goto, and often needless.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #33
In article <b6************ **************@ posting.google. com>,
nz******@cs.mun .ca (Neil Zanella) wrote:
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote in message
(And some people will be in for some nasty surprises if they switch to a
compiler where unsigned int and unsigned long have different sizes. )


Such as...?


unsigned long x = -1u;

You might expect this to create the largest possible unsigned long, but
it doesn't if unsigned long has more bits than unsigned int. Until quite
recently the machines where this is the case have become more rare (16
bit vs. 32 bit), but now the number starts growing again (32 bit vs. 64)
Nov 14 '05 #34
> > > For signed integers, the C operators +, -, * produce exactly the same
results as the mathematical operators (as long as the results are not
too large).
For unsigned integers, the C operators do some pretty weird
things. A trivial example: For which numbers is

x >= y - 1

true if x and y are both signed, both unsigned, one signed and the other
unsigned? For signed numbers, you are quite safe. Both unsigned, and
there is a strange special case for y = 0.


For signed ints, there is a strange special case for y = INT_MIN. With
unsigned integers, you have well-defined over/underflow semantics at
least, but as long as you stay within the range of the type, you are
safe with both signed and unsigned integers.


Which case happens more often, INT_MIN or 0?


If it is code relating to my bank balance: INT_MIN :)
My point: this problem pertains to any sort of programming
with range-limited integral types. Every time you perform
an arithmetic operation you must consider the overflow cases.
Each case is on its own merits and in the context of the
application (eg. deciding whether you are more likely to have
problems with 0, INT_MIN or UINT_MAX).

If I judge that the code will never be near an overflow (unless
the code has UB'd already) then I will write something as you
did above. If it is important to notice overflows then I will
write some small functions to perform the operation and handle
overflow in a way appropriate for that application. (for example,
in code dealing with # of seconds since 1970, or # of milliseconds
since whenever).
I have a couple of other rules of thumb:
- enable compiler warnings for signed vs. unsigned comparison
- prefer to use + instead of - (I find it easier to comprehend)
x + 1 >= y

NB. Of course these are all my own guidelines to avoid overflow
bugs, they should not be interpreted as expert advice
Nov 14 '05 #35
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:

The fact that size_t is unsigned is a real pain in the ass, because
this type is seldom used in a genuine unsigned context. It should
have been signed, for the same reason that argc is signed.


There are two fundamental problems without compatible resolution.
First, int to unsigned conversion is always possible, while the
reverse may not be. This encourages the use of ints. Secondly,
size_t usually represents things that can be addressed in the
system, and this is very unlikely to waste any bit positions.
This more or less mandates the use of size_t (and thus unsigned)
in many places.


How many times have you defined an object occupying half of your address
space or more?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #36
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:

The fact that size_t is unsigned is a real pain in the ass, because
this type is seldom used in a genuine unsigned context. It should
have been signed, for the same reason that argc is signed.


There are two fundamental problems without compatible resolution.
First, int to unsigned conversion is always possible, while the
reverse may not be. This encourages the use of ints. Secondly,
size_t usually represents things that can be addressed in the
system, and this is very unlikely to waste any bit positions.
This more or less mandates the use of size_t (and thus unsigned)
in many places.


How many times have you defined an object occupying half of your
address space or more?


Quite often. Of course it wasn't in C, and it didn't have large
virtual memory.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #37
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:
> Dan Pop wrote:
>>
>> The fact that size_t is unsigned is a real pain in the ass, because
>> this type is seldom used in a genuine unsigned context. It should
>> have been signed, for the same reason that argc is signed.
>
>There are two fundamental problems without compatible resolution.
>First, int to unsigned conversion is always possible, while the
>reverse may not be. This encourages the use of ints. Secondly,
>size_t usually represents things that can be addressed in the
>system, and this is very unlikely to waste any bit positions.
>This more or less mandates the use of size_t (and thus unsigned)
>in many places.


How many times have you defined an object occupying half of your
address space or more?


Quite often. Of course it wasn't in C, and it didn't have large
virtual memory.


If it wasn't in C, why would it be relevant to size_t's type?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #38
On Tue, 10 Feb 2004 17:21:31 GMT, in comp.lang.c , CBFalconer
<cb********@yah oo.com> wrote:
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:
> Dan Pop wrote:
>>
>> The fact that size_t is unsigned is a real pain in the ass, because
>> this type is seldom used in a genuine unsigned context. It should
>> have been signed, for the same reason that argc is signed.
>
>There are two fundamental problems without compatible resolution.
>First, int to unsigned conversion is always possible, while the
>reverse may not be. This encourages the use of ints. Secondly,
>size_t usually represents things that can be addressed in the
>system, and this is very unlikely to waste any bit positions.
>This more or less mandates the use of size_t (and thus unsigned)
>in many places.


How many times have you defined an object occupying half of your
address space or more?


Quite often. Of course it wasn't in C, and it didn't have large
virtual memory.


FWIW in the olden days, when 512K was standard on Amstrad PCs, I did find
myself allocating 256K in one go. Hardly a truly enormous object when you
think about it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #39
In article <c0**********@s unnews.cern.ch> , Da*****@cern.ch (Dan Pop)
wrote:
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:

The fact that size_t is unsigned is a real pain in the ass, because
this type is seldom used in a genuine unsigned context. It should
have been signed, for the same reason that argc is signed.


There are two fundamental problems without compatible resolution.
First, int to unsigned conversion is always possible, while the
reverse may not be. This encourages the use of ints. Secondly,
size_t usually represents things that can be addressed in the
system, and this is very unlikely to waste any bit positions.
This more or less mandates the use of size_t (and thus unsigned)
in many places.


How many times have you defined an object occupying half of your address
space or more?


And if you have done so, did you run into problems because ptrdiff_t is
not capable of representing differences between pointers in all cases?
Nov 14 '05 #40

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

Similar topics

9
7031
by: Ton den Hartog | last post by:
I am confused, when use ID and when use NAME to identify an element in HTML ? Ton den Hartog -- Computer museum tonh: http://www.tonh.net - 10.000 visitors !! GGGallery website generator: http://www.tonh.net/gggallery
10
10833
by: bg_ie | last post by:
Hi, I have a function which compares two unsigned ints and returns their difference in order to establish which is the greater of the two. int Compare(unsigned int time_left, unsigned int time_right) { return (time_left - time_right); }
0
11236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9641
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5855
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6061
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.