473,788 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Max value of an integer type?

I'm writing a template, and I need to know the maximum value of a given
integer type at compile time. something like:

template<class NumType>
class Arb {
public:

static NumType const max = /* maximum value */;

};
If the template were only to be used with unsigned integer types, then
I'd do the following:

template<class NumType>
class Arb {
public:

static NumType const max = -1;

};
I need a compile-time constant which evaluates to the maximum value of an
integer type.

Any ideas?

If it weren't undefined behaviour to overflow a signed integer, I could
use a metaprogramming technique such as the following:

template<class T, bool overflow = false>
struct MaxIntVal {
private:

static T const internal = 1 + MaxIntVal<T,...

public:

static T const val = SCHAR_MAX + internal;
};
--

Frederick Gotham
Jul 4 '06
35 12588
* Frederick Gotham:
Alf P. Steinbach posted:
>Note the bit pattern for the highest value when using a given number of
bits.

I was under the assumption that C++ had to store positive values as
follows:

0: 0000
1: 0001
2: 0010
3: 0011
4: 0100
5: 0101
6: 0110
7: 0111
8: 1000
9: 1001
10: 1010
11: 1011
12: 1100
13: 1101
14: 1110
15: 1111

Are you saying that:

(A) The machine could use another method such as "Gray code".
and
(B) My code could break on other systems, and thus isn't trully
portable?
It's just academic, but yes. §5.8/2 defines the result of << as a
bit-level operation, and as a value only for unsigned types. §5.8/3
defines the result of >as a bit-level operation and as a value both
for signed and unsigned types (the latter only for non-negative values).
The cases excluded from these definitions are, AFAICT, exactly those
where the definitions would otherwise be incompatible with grey-code...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #21
* Rolf Magnus:
Rolf Magnus wrote:
>Alf P. Steinbach wrote:
>>It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)
Hmm, I would have said 3.9.1/7, but after reading it again, it seems that
this actually doesn't prohibit a grey-code representation.

Ok, now I have one: 5.8/2.
Nope, not as far as I can see: it's /very/ ingeniously worded so that
the cases that could be incompatible are simply not defined.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #22
* Alf P. Steinbach:
grey-code
Sorry, again: _gray code_.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #23
Frederick Gotham wrote:
Rolf Magnus posted:

>Ok, now I have one: 5.8/2.

The value of E1 << E2 is E1 (interpreted as a bit pattern) left-shifted E2
bit positions; vacated bits are zero-filled. If E1 has an unsigned type,
the value of the result is E1 multiplied by the quantity 2 raised to the
power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned long,
UINT_MAX+1 otherwise.
Is it the "vacated bits are zero-filled" part that you're talking about?
No, it's the "(interpret ed as a bit pattern) left-shifted E2 bit positions"
in combination with the "multiplied by the quantity 2 raised to the power
E2". If your values are represented in gray code, a left-shift of the bit
pattern doesn't necessarily result in a mulitplication by 2.

Jul 5 '06 #24
* Rolf Magnus:
Frederick Gotham wrote:
>Rolf Magnus posted:

>>Ok, now I have one: 5.8/2.
The value of E1 << E2 is E1 (interpreted as a bit pattern) left-shifted E2
bit positions; vacated bits are zero-filled. If E1 has an unsigned type,
the value of the result is E1 multiplied by the quantity 2 raised to the
power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned long,
UINT_MAX+1 otherwise.
Is it the "vacated bits are zero-filled" part that you're talking about?

No, it's the "(interpret ed as a bit pattern) left-shifted E2 bit positions"
in combination with the "multiplied by the quantity 2 raised to the power
E2". If your values are represented in gray code, a left-shift of the bit
pattern doesn't necessarily result in a mulitplication by 2.
That's only for unsigned types. C++ unsigned integers are pure binary.
For signed integers there's much more leeway (formally).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #25
Alf P. Steinbach wrote:
* Rolf Magnus:
>Rolf Magnus wrote:
>>Alf P. Steinbach wrote:

It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)
Hmm, I would have said 3.9.1/7, but after reading it again, it seems
that this actually doesn't prohibit a grey-code representation.

Ok, now I have one: 5.8/2.

Nope, not as far as I can see: it's /very/ ingeniously worded so that
the cases that could be incompatible are simply not defined.
What's not defined? It says clearly that the bit pattern is shifted left by
the specified number of bits and that this will (for unsigned types) result
in a multiplication by 2 raised to the value of the rhs. That's not
possible with Gray code, so unsigned types cannot use Gray code. Since a
bit pattern for a signed value must be the same as for the corresponding
unsigned value, signed types neither can use Gray code.

Jul 5 '06 #26
Alf P. Steinbach wrote:
* Rolf Magnus:
>Frederick Gotham wrote:
>>Rolf Magnus posted:
Ok, now I have one: 5.8/2.
The value of E1 << E2 is E1 (interpreted as a bit pattern) left-shifted
E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned
type, the value of the result is E1 multiplied by the quantity 2 raised
to the power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned
long, UINT_MAX+1 otherwise.
Is it the "vacated bits are zero-filled" part that you're talking about?

No, it's the "(interpret ed as a bit pattern) left-shifted E2 bit
positions" in combination with the "multiplied by the quantity 2 raised
to the power E2". If your values are represented in gray code, a
left-shift of the bit pattern doesn't necessarily result in a
mulitplicati on by 2.

That's only for unsigned types. C++ unsigned integers are pure binary.
For signed integers there's much more leeway (formally).
Only for the negative values. So I guess a signed integer could use Gray
code for the negative part. That would be extremely odd, but seems to be
allowed by the standard.
Jul 5 '06 #27
* Rolf Magnus:
Alf P. Steinbach wrote:
>* Rolf Magnus:
>>Frederick Gotham wrote:

Rolf Magnus posted:
Ok, now I have one: 5.8/2.
The value of E1 << E2 is E1 (interpreted as a bit pattern) left-shifted
E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned
type, the value of the result is E1 multiplied by the quantity 2 raised
to the power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned
long, UINT_MAX+1 otherwise.
Is it the "vacated bits are zero-filled" part that you're talking about?
No, it's the "(interpret ed as a bit pattern) left-shifted E2 bit
positions" in combination with the "multiplied by the quantity 2 raised
to the power E2". If your values are represented in gray code, a
left-shift of the bit pattern doesn't necessarily result in a
mulitplicatio n by 2.
That's only for unsigned types. C++ unsigned integers are pure binary.
For signed integers there's much more leeway (formally).

Only for the negative values. So I guess a signed integer could use Gray
code for the negative part. That would be extremely odd, but seems to be
allowed by the standard.
No, it's for any (signed) value. Left shift doesn't define the numeric
value. And right shift >does define the value but is all OK.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #28
* Rolf Magnus:
Alf P. Steinbach wrote:
>* Rolf Magnus:
>>Rolf Magnus wrote:

Alf P. Steinbach wrote:

It's good fun to throw academic spanners into practical wheels, so,
where does the standard exclude a grey-code representation? ;-)
Hmm, I would have said 3.9.1/7, but after reading it again, it seems
that this actually doesn't prohibit a grey-code representation.
Ok, now I have one: 5.8/2.
Nope, not as far as I can see: it's /very/ ingeniously worded so that
the cases that could be incompatible are simply not defined.

What's not defined? It says clearly that the bit pattern is shifted left by
the specified number of bits and that this will (for unsigned types) result
in a multiplication by 2 raised to the value of the rhs. That's not
possible with Gray code, so unsigned types cannot use Gray code. Since a
bit pattern for a signed value must be the same as for the corresponding
unsigned value, signed types neither can use Gray code.
No, the bit pattern for a signed value must not necessarily be the same
as for the corresponding unsigned value. In particular, that very
obviously doesn't hold for negative values. And by extension, it
doesn't hold for non-negative values either (if you're looking at the
same paragraph which has been discussed to death in this connection
before [the solution, if it may be called that, is then to look at the
definitions of terms] ;-)).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #29
Alf P. Steinbach wrote:
* Rolf Magnus:
>Alf P. Steinbach wrote:
>>* Rolf Magnus:
Rolf Magnus wrote:

Alf P. Steinbach wrote:
>
>It's good fun to throw academic spanners into practical wheels, so,
>where does the standard exclude a grey-code representation? ;-)
Hmm, I would have said 3.9.1/7, but after reading it again, it seems
that this actually doesn't prohibit a grey-code representation.
Ok, now I have one: 5.8/2.
Nope, not as far as I can see: it's /very/ ingeniously worded so that
the cases that could be incompatible are simply not defined.

What's not defined? It says clearly that the bit pattern is shifted left
by the specified number of bits and that this will (for unsigned types)
result in a multiplication by 2 raised to the value of the rhs. That's
not possible with Gray code, so unsigned types cannot use Gray code.
Since a bit pattern for a signed value must be the same as for the
correspondin g unsigned value, signed types neither can use Gray code.

No, the bit pattern for a signed value must not necessarily be the same
as for the corresponding unsigned value. In particular, that very
obviously doesn't hold for negative values. And by extension, it
doesn't hold for non-negative values either (if you're looking at the
same paragraph which has been discussed to death in this connection
before [the solution, if it may be called that, is then to look at the
definitions of terms] ;-)).
You must be talking about:

"The range of nonnegative values of a signed integer type is a subrange of
the corresponding unsigned integer type, and the value representation of
each corresponding signed/unsigned type shall be the same."

Not sure what's wrong about that, though. Doesn't it mean that the same
value in a corresponding signed and unsigned type must have the same bit
pattern? Obviously, that's only possible for the subrange that is covered
by both.

Jul 5 '06 #30

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

Similar topics

20
4851
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
4
1831
by: Semi Head | last post by:
Hello folks, I'm looking for a script to validate a specific number value in a standard form input field. An example would be, if someone enters a number into a form input, I want the script to validate it and give an alert if that the number exceeds the set script value. Like if the script value is set for 3000 and the number 3002 is entered, I want an alert to pop and give a warning, BTW - This script must be generic enough to work...
12
3222
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
4
2561
by: Chris Bower | last post by:
Reposted from aspnet.buildingcontrols: Ok, I've got a bunch of derived controls that all have a property Rights of type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so that I can use comma separated values in design-time. The TypeConverter works great in design-time. It converts to and from just fine... However, when I try to load any page in the site now I get the following error (Following the error is code for the...
2
7213
by: Jim in Arizona | last post by:
I'm learning form an ASP.NET 1.0 book and I tried out some code that returns this error: Compiler Error Message: BC30311: Value of type 'Integer' cannot be converted to 'ASP.multiclasses_aspx.VehicleKey'. Source Error: Line 106:
2
1683
by: Arne | last post by:
Will the dataset below be returned by value or reference? Public Shared Function getDS() As DataSet Dim ds As New DataSet '... do something Return ds End Function
20
3720
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If MyVariant = 2 Then MsgBox "MyVariant also equals the value 2" 160 If MyVariant = "" Then HowManyCopies = 1 170 If Not IsNumeric(MyVariant) Then HowManyCopies = 1 MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCopies) 180 For i =...
23
2197
by: Tomás | last post by:
Anything wrong with the following code?: #include <cstdlib> int main() { for (unsigned i = 0; i != 1000; ++i) { int *p = reinterpret_cast<int*>( std::rand() );
22
27077
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
14
1772
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
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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
5398
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...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.