473,323 Members | 1,560 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,323 software developers and data experts.

Integer promotion?

On my system, the following doesn't work properly:

#define UCHAR_MAX ( ~( (unsigned char)0 ) )
Could someone please explain why it doesn't work? My hunch is that it has
something to do with integer promotion.
Since the previous example doesn't work, why is it that the following
does?

template<class AnyUnsignedIntegerType>
AnyUnsignedIntegerType MaxVal()
{
return ~AnyUnsignedIntegerType(0);
}

#include <iostream>

int main()
{
std::cout << (unsigned long) MaxVal<unsigned char>() << "\n\n";

/* Cast required so it prints as a number rather
than as a character */
}
--

Frederick Gotham
Jun 9 '06 #1
1 1970
Frederick Gotham wrote:
On my system, the following doesn't work properly:

#define UCHAR_MAX ( ~( (unsigned char)0 ) )
Could someone please explain why it doesn't work? My hunch is that it
has something to do with integer promotion.
Macros have no type. If you try using your 'UCHAR_MAX' macro (is there
a reason you used the standard name? You know you're not allowed to,
right?) the 0 is promoted to 'int', then one's complement is obtained,
then the value "all bits set" is used. If your int is 16 bits, you get
65535.
Since the previous example doesn't work, why is it that the following
does?

template<class AnyUnsignedIntegerType>
AnyUnsignedIntegerType MaxVal()
{
return ~AnyUnsignedIntegerType(0);
}

#include <iostream>

int main()
{
std::cout << (unsigned long) MaxVal<unsigned char>() << "\n\n";

/* Cast required so it prints as a number rather
than as a character */
}


'MaxVal' returns a value of a particular type (the one you instantiated
it with). Whatever the value is inside in the 'return' statement, it
gets converted (in your case most likely truncated) to 'unsigned char'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 9 '06 #2

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

Similar topics

21
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me....
4
by: spibou | last post by:
On 6.3.1.1 of N1124 we read: If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. A few lines above that...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
34
by: Cuthbert | last post by:
Hi folks, I am trying to find a more efficient way to count "How many bits are '1' in a integer variable?". I still have no idea to count the bits except using a loop and "if" statements....
1
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a...
2
by: Spoon | last post by:
Consider the following program: #include <stdint.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { uint32_t a = strtoul(argv, 0, 0); uint32_t b = strtoul(argv, 0,...
9
by: Fred | last post by:
I'm having terrible trouble trying to work out exactly which of the promotions one reads about in old books are still present in current C - and there seems to be a distinction between promotion...
2
by: Sune | last post by:
Hi all, there are several situations where integer promotion is performed in C and I need to confirm my understanding of it. Let's contain the discussion to workstation/server CPUs of 32/64...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
9
by: tsuyois | last post by:
Hi, I just signed in to this excellent network. I hope I could get some answers to many questions I have in writing C compilers. My first question is: Is "integer demotion" required in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.