What's a portable value for UINT_MIN?
Asked Answered
S

3

10

In limits.h, there are #defines for INT_MAX and INT_MIN (and SHRT_* and LONG_* and so on), but only UINT_MAX.

Should I define UINT_MIN myself? Is 0 (positive zero) a portable value?

Shook answered 17/8, 2010 at 4:24 Comment(2)
+1 for caring enough about portability and correctness to ask.Tendril
I should give a -1 for senselessly wanting to remove the "magic number" 0. But oh well.Oakley
F
4

If you want to be "typesafe" you could use 0U, so if you use it in an expression you will have the correct promotions to unsigned.

Fearsome answered 17/8, 2010 at 9:33 Comment(7)
So I should #define UINT_MIN +0U in limits.h?Shook
Thinking of it, it is perhaps not so a good idea to have a file that has the same name as a standard file or to mess around with the later. I'd put it another place. If you want to have a + to the 0U (why?) you should have the whole in parenthesis (+0U) since otherwise you might have unwanted effects when the + could be interpreted as addition.Fearsome
You don't need parenthesis. Even in 1+UINT_MIN, it would expand to 1+ +0U, which is a correct expression. Macro expansion happens at token level, and will not glue the two plusses together.Prevention
@MSalters: I think you need it since it makes invalid sequences valid. Something like 5 UINT_MIN would become valid, where it really shouldn't.Fearsome
@Jens: Using that logic, I can equally say that you must not have parenthesis as it makes invalid sequences valid. Something like sin UINT_MIN becomes valid.Prevention
@MSalters: good point. So it is a matter of taste which invalid sequences should become valid ;-) Brings us back to the question, why using the + in the first place? 0U would be much simpler, then, and is really a well defined constant.Fearsome
++ for the pedantic type safety note; you tickled my humour :)Harp
A
21

It's an unsigned integer - by definition its smallest possible value is 0. If you want some justification besides just common sense, the standard says:

6.2.6.2 Integer types

  1. For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N−1, so that objects of that type shall be capable of representing values from 0 to 2N− 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.
Alphard answered 17/8, 2010 at 4:25 Comment(5)
love the standard. especially when it's quoted.Primula
This does not answer my question.Shook
@Ariel, how does it not answer your question? The minimum value for an unsigned type is defined by the standard to be 0.Alphard
the last 2^(N-1) should be (2^N)-1 instead e.g., for N=8 UCHAR_MAX = (2^8)-1 = 255Smyrna
@EricPostpischil fixed. For my first edit I tried to fix only the typo but there wasn't enough characters changed, so I had to change something else.Babita
S
7

You could use std::numeric_limits<unsigned int>::min().

Simulation answered 17/8, 2010 at 4:56 Comment(2)
This will work in C++, and this will not work in C. What is C/C++?Simulation
Our source tree contains C and C++ and so this need to work with both. This is why I tagged the question with both.Shook
F
4

If you want to be "typesafe" you could use 0U, so if you use it in an expression you will have the correct promotions to unsigned.

Fearsome answered 17/8, 2010 at 9:33 Comment(7)
So I should #define UINT_MIN +0U in limits.h?Shook
Thinking of it, it is perhaps not so a good idea to have a file that has the same name as a standard file or to mess around with the later. I'd put it another place. If you want to have a + to the 0U (why?) you should have the whole in parenthesis (+0U) since otherwise you might have unwanted effects when the + could be interpreted as addition.Fearsome
You don't need parenthesis. Even in 1+UINT_MIN, it would expand to 1+ +0U, which is a correct expression. Macro expansion happens at token level, and will not glue the two plusses together.Prevention
@MSalters: I think you need it since it makes invalid sequences valid. Something like 5 UINT_MIN would become valid, where it really shouldn't.Fearsome
@Jens: Using that logic, I can equally say that you must not have parenthesis as it makes invalid sequences valid. Something like sin UINT_MIN becomes valid.Prevention
@MSalters: good point. So it is a matter of taste which invalid sequences should become valid ;-) Brings us back to the question, why using the + in the first place? 0U would be much simpler, then, and is really a well defined constant.Fearsome
++ for the pedantic type safety note; you tickled my humour :)Harp

© 2022 - 2024 — McMap. All rights reserved.