473,406 Members | 2,633 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,406 software developers and data experts.

Largest value of an unsigned integral type


Hello all,

Suppose you have an unsigned integral type T. It's not one of the built-in
types, but rather typedefed off of one of the built-in unsigned integral
types (but we don't know which one).

I want to find the maximum value for this type. This seems to work just
fine:

static_cast<T>(-1)

Is there any reason this should be avoided? Or is this indeed guaranteed to
do the job on a standard-conforming compiler?

I'm not using numeric_limits<> to get the maximum value because, as stated
above, I don't know the underlying built-in type, so I don't know which
specialization of numeric_limits<> to use.

In case anyone is wondering, the type I'm actually working with is
string::size_type. I need to get the maximum value for this type, but,
according to Josuttis, I can only be assured the underlying type is unsigned
integral, but there are no guarantees as to which of the unsigned integral
types it is...

Does this look clean to everyone?

Thanks,
Dave
Jul 19 '05 #1
3 6204
On Thu, 6 Nov 2003 20:53:30 -0700, "Dave" <be***********@yahoo.com>
wrote in comp.lang.c++:

Hello all,

Suppose you have an unsigned integral type T. It's not one of the built-in
types, but rather typedefed off of one of the built-in unsigned integral
types (but we don't know which one).

I want to find the maximum value for this type. This seems to work just
fine:

static_cast<T>(-1)

Is there any reason this should be avoided? Or is this indeed guaranteed to
do the job on a standard-conforming compiler?


This is guaranteed to work. The conversion of any a value of any
integer type to any unsigned integer type is well defined. If the
value being assigned is outside the range of the destination unsigned
type, it is adjusted by repeatedly adding or subtracting (UTYPE_MAX +
1) until the value is within the range [0...UTYPE_MAX], so converting
-1 to any unsigned yields the maximum value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #2
Dave wrote:
...
Suppose you have an unsigned integral type T. It's not one of the built-in
types, but rather typedefed off of one of the built-in unsigned integral
types (but we don't know which one).

I want to find the maximum value for this type. This seems to work just
fine:

static_cast<T>(-1)

Is there any reason this should be avoided? Or is this indeed guaranteed to
do the job on a standard-conforming compiler?
Yes, it is guaranteed to do the job.
I'm not using numeric_limits<> to get the maximum value because, as stated
above, I don't know the underlying built-in type, so I don't know which
specialization of numeric_limits<> to use.
I don't understand why do you care about knowing "the underlying
built-in type". From the above 'static_cast' example it follows that you
know the typedef-name of the type, don't you? You can immediately use it
with 'std::numeric_limits'

std::numeric_limits<T>::max();
In case anyone is wondering, the type I'm actually working with is
string::size_type. I need to get the maximum value for this type, but,
according to Josuttis, I can only be assured the underlying type is unsigned
integral, but there are no guarantees as to which of the unsigned integral
types it is...


You can simply use 'std::numeric_limits<std::string::size_type>::max( )'.

Actually, 'std::string::npos' is guaranteed to represent the largest
value of 'std::string::size_type' (it is initialized by using the same
trick with '-1')

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3
"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...

Hello all,

Suppose you have an unsigned integral type T. It's not one of the built-in types, but rather typedefed off of one of the built-in unsigned integral
types (but we don't know which one).

I want to find the maximum value for this type. This seems to work just
fine:

static_cast<T>(-1)

Is there any reason this should be avoided? Or is this indeed guaranteed to do the job on a standard-conforming compiler?

I'm not using numeric_limits<> to get the maximum value because, as stated
above, I don't know the underlying built-in type, so I don't know which
specialization of numeric_limits<> to use.
Sure you do. numeric_limits <T> works. If this would not work, then the
static_cast would not work either, there, too, you have to know T at compile
time. Think of it as a template function. Maybe it is clearer then.
In case anyone is wondering, the type I'm actually working with is
string::size_type. I need to get the maximum value for this type, but,
according to Josuttis, I can only be assured the underlying type is unsigned integral, but there are no guarantees as to which of the unsigned integral
types it is...

Does this look clean to everyone?


hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #4

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

Similar topics

5
by: Dave Rahardja | last post by:
I've tried looking this topic up in the standard manual but came up empty... 1. What is the value of an unsigned integral type after it is decremented below zero? 2. What is the value of an...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
30
by: Alf P. Steinbach | last post by:
The C++ FAQ item 29.5 (this seems to be strongly related to C), at <url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5> mentions that <quote> C++ guarantees a char is exactly one...
12
by: Francois Grieu | last post by:
The values of ((int)0.7) and ((int)-0.7) seem to be 0 Is this independent of implementation ? TIA, François Grieu
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
18
by: Susan Rice | last post by:
I'm comparing characters via return(str1 - str2); and I'm having problems with 8-bit characters being treated as signed instead of unsigned integers. The disassembly is using movsx ...
12
by: Ahmad Jalil Qarshi | last post by:
Hi, I have an integer value which is very long like 9987967441778573855. Now I want to convert it into equivalent Hex value. The result must be 8A9C63784361021F I have used...
15
by: RezaRob | last post by:
gcc is clearly returning -1 when I do this ~ (unsigned short) 0 which is not the case if "unsigned int" is used instead of "unsigned short". What does the C standard say about that? ...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.