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

Why does 'uint64_t i = -UINT64_MAX' have value '1'?

What is the value of -(max-val-of-an-unsigned-type)? According to my
compiler, -UINT32_MAX = 1, and -UINT64_MAX is also 1 (test code
below).

From the C99 LRM, p79:

"The result of the unary - operator is the negative of its (promoted)
operand. The integer promotions are performed on the operand, and the
result has the promoted type."

As far as I can make out from p43, uint32_t and uint64_t are unchanged
by the integer promotions. So how do you take the negative of an
unsigned operand, and how can the compiler get the result '1' without
using a temporary of a higher precision? Any thoughts?

Thanks -

Dom

--------------------------------------------------------------
[the code below is C++ to simplify data output, and produces this
result]:

i is ffffffff; j is 1; k is 1; l is ffffffffffffffff; -l is 1; m is 1

--------------------------------------------------------------

#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <iostream>
#include <iomanip>

int main() {
uint32_t i = UINT32_MAX;
int64_t j;
uint64_t k;
uint64_t l = UINT64_MAX;
uint64_t m = -UINT64_MAX;

j = -i;
k = -i;

std::cout << std::hex << std::setfill('0')
<< "i is " << i
<< "; j is " << j
<< "; k is " << k
<< "; l is " << l
<< "; -l is " << -l
<< "; m is " << m
<< std::endl;

return 0;
}
Jun 23 '07 #1
6 7165
Dom Jackson said:
What is the value of -(max-val-of-an-unsigned-type)?
In C (and presumably in C++ too), arithmetic on unsigned integer types
is carried out modulo the maximum value of the type + 1. If you try to
store a negative number in an unsigned integer type, exactly enough
"maximum value of the type, + 1"s are added to bring it into the range
representable by the type.

So let's pretend there's a four-bit unsigned integer type: unsigned
nibble. :-) UNIBBLE_MAX is 15, so UNIBBLE_MAX + 1 is 16.

-UNIBBLE_MAX would be -15, so we have to add 16 to that to bring it into
the representable range, and -15 + 16 is 1.

Now let's think of an arbitrary unsigned integer type, unsigned arb.
UARB_MAX has some value or other, doesn't matter what.

-UARB_MAX is clearly negative, so we need to add enough lots of
(UARB_MAX + 1) to bring it into the representable range. One is enough,
so the resulting value is -UARB_MAX + UARB_MAX + 1, which is clearly 1,
irrespective of the value of UARB_MAX.

<C++ code snipped>

When cross-posting between comp.lang.c and comp.lang.c++, please ensure
that your code snippets are relevant to both groups. Thanks.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 23 '07 #2
That's what I call service - thanks!

On Sat, 23 Jun 2007 18:32:41 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>So let's pretend there's a four-bit unsigned integer type: unsigned
nibble. :-)
Actually, that's precisely why I needed to know the answer, although
slightly more generally.
>-UNIBBLE_MAX would be -15, so we have to add 16 to that to bring it into
the representable range, and -15 + 16 is 1.
Ahhh... I tried this with a 3-bit int, but I was trying to add 8 to
bit pattern '111', rather than to integer -7, which makes a lot more
sense.
>When cross-posting between comp.lang.c and comp.lang.c++, please ensure
that your code snippets are relevant to both groups. Thanks.
I would have, if I'd known of portable printf conversions for 32-bit
and 64-bit ints - you don't happen to know the answer to that one?

Thanks -

Dom
Jun 23 '07 #3
Dom Jackson said:
That's what I call service - thanks!
No sweat.

<snip>
>>When cross-posting between comp.lang.c and comp.lang.c++, please
ensure that your code snippets are relevant to both groups. Thanks.

I would have, if I'd known of portable printf conversions for 32-bit
and 64-bit ints - you don't happen to know the answer to that one?
C provides two integer types that are guaranteed to be at least 32 bits
wide: long int, and unsigned long int. The printf function can display
long int correctly using "%ld", and unsigned long using "%lu". The
Standard doesn't mandate a maximum size for these types, so they might
easily be 64 bits wide on some systems, but 32 bits is all you're
guaranteed.

C99 (which hardly anyone has a compiler for) introduced two new integer
types that are guaranteed to be at least 64 bits wide: long long int
and unsigned long long int. Their printf format specifiers are,
respectively, "%lld" and "%llu". Don't hold your breath waiting for
long long and unsigned long long to become portable, though.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 23 '07 #4
Thanks again -

Dom
Jun 23 '07 #5
Dom Jackson wrote:
Richard Heathfield <rj*@see.sig.invalidwrote:
.... snip ...
>
>When cross-posting between comp.lang.c and comp.lang.c++, please
ensure that your code snippets are relevant to both groups. Thanks.

I would have, if I'd known of portable printf conversions for 32-bit
and 64-bit ints - you don't happen to know the answer to that one?
printf doesn't do such conversions. The format string tells it the
actual format of each parameter.

Follow-ups set to eliminate com.lang.c++. Bad cross-post.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 24 '07 #6
Dom Jackson wrote:
>>When cross-posting between comp.lang.c and comp.lang.c++, please ensure
that your code snippets are relevant to both groups. Thanks.

I would have, if I'd known of portable printf conversions for 32-bit
and 64-bit ints - you don't happen to know the answer to that one?
There is none for C++. In C(99), you can do:

#include <stdint.h>
#include <stdio.h>
#include <inttypes.h>

int main()
{
int64_t value = 123;
printf("The value is %" PRId64 "\n", value);
}

Jun 24 '07 #7

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

Similar topics

36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
0
by: Faisal | last post by:
I have a question regarding the "PublicKeyToken" attribute of the "<section>" tag in the web.config file. Pretty much most references have this value set to "b77a5c561934e089"; the same value as in...
10
by: MLH | last post by:
I have an A97 table with a Yes/No field named TowJob and a form bound to that table. The TowJob control on the form is bound to the same field. It is an option group with Yes and No bttns valued...
1
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
25
by: guy | last post by:
i have inherited the following migrated vb6 code (vb2005) but i DONT get the "not all paths return a value" squiggly - is this a 'feature' of on error goto? Private Function CreateFolder(ByVal...
0
by: mbaril68 | last post by:
I have a combobox with the Text property set to the field I want. It displays and saves correctly until I try to use a dataset to show the list. I set DataSource to the dataset with the list and...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
2
by: allan.palmer | last post by:
Hi All, I have a text box that is currently being set by a javascript function as a running total. the javascript is working fine, as values are changing the running total is being set...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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...
0
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...

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.