Solved

max value

Posted on 2013-10-25
17
476 Views
Last Modified: 2013-10-27
in both cases, I am crossing the limit but the outputs are different.

1) Why is the SUM value 1 less than LOW
2) Why is maxeof_2 = 0

	uint64_t LOW = 9223372036854775807;   1<<63 - 1; 
	uint64_t LEN = 18446744073709551615;
	uint64_t SUM = LOW + LEN;

	printf  ("Low = %" PRIu64 "\n", LOW);
	printf  ("LEN = %" PRIu64 "\n", LEN);
	printf  ("SUM = %" PRIu64 "\n", SUM);
	printf  ("SIZE of off_t = %u \n", sizeof (off_t));	

	static off_t maxeof_2 = ((off_t)1 << (sizeof (off_t) * 8 - 1)) - 1;  // (1 << 63) - 1
        printf ("maxeof = %" PRIu64 "\n", maxeof_2);

	static off_t maxeof_2 = ((off_t)1 << (sizeof (off_t) * 8 ));  // (1 << 64)
        printf ("maxeof_2 = %" PRIu64 "\n", maxeof_2);

Open in new window



Low = 9223372036854775807
LEN = 18446744073709551615
SUM = 9223372036854775806
SIZE of off_t = 8
maxeof = 9223372036854775807
maxeof_2 = 0
0
Comment
Question by:perlperl
  • 9
  • 8
17 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39600927
For 1.: 9223372036854775807 already is the maximum value a uint64_t can hold. Regardless of what you add, the result that is cased by the overflow will be less than that maximum value.

For2.:  You are shifting that over the last bit that off_t can holt. Since the bit is not 'rotated' back in, the result is '0', since no bits are set to '1' in that value.
0
 

Author Comment

by:perlperl
ID: 39600954
Shouldn't the value of SUM must be 18446744073709551615  (1 << 64 - 1)
1<<64 -1 is the max value uint64 can hold and not 9223372036854775807.

18446744073709551615 =  All 64 bits 1
9223372036854775807   =  MSB is 0 and Lower 63 bits 1.
9223372036854775808   =  MSB is 1 and Lower 63 bits 0.

So based on this SUM must be  18446744073709551615 ??
0
 
LVL 86

Expert Comment

by:jkr
ID: 39600970
No, it's not like a glass that you fill up and the rest overpours. The overflow will result in a value that is 'value_you_add - UI64_MAX'
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 86

Expert Comment

by:jkr
ID: 39600978
Ooops, correction, that should be "The overflow will result in a value that is 'UI63_MAX - value_you_add'''
0
 

Author Comment

by:perlperl
ID: 39600992
I am still confused ;)
how does Ui63_MAX - value_you_add  (UI63_MAX = 9223372036854775807. I am not adding 1)
maps to 9223372036854775806
0
 
LVL 86

Expert Comment

by:jkr
ID: 39601011
Um, that's not UI64_MAX. 9223372036854775807 is 0x7FFFFFFFFFFFFFFF, whereas UI64_MAX is 0xFFFFFFFFFFFFFFFF aka 18446744073709551615. 0x7FFFFFFFFFFFFFFF is I64_MAX.
0
 

Author Comment

by:perlperl
ID: 39601141
I understand that but how SUM value is 9223372036854775806

uint64_t SUM = LOW + LEN;
                       = 9223372036854775807 + 18446744073709551615;
                       = 9223372036854775806


  which is
SUM = 0x7FFFFFFFFFFFFFFF + 0xFFFFFFFFFFFFFFFF
         = 0x7FFFFFFFFFFFFFFF - 1   (How in the world compiler does this ?  ;)    )
          = 0x6FFFFFFFFFFFFFFF
0
 
LVL 86

Expert Comment

by:jkr
ID: 39601318
>> (How in the world compiler does this ?  ;)    )

That's not the compiler, that's the CPU at runtime adhereing to the rules of binary arithmetics.
0
 

Author Comment

by:perlperl
ID: 39601867
my bad.
but why -1 of LOW ;)
0
 
LVL 86

Expert Comment

by:jkr
ID: 39601923
Um, sorry, missed that - it's also not 'LOW - 1', because that would be equal to 0x7FFFFFFFFFFFFFFE, the difference is actually 0x1000000000000000 or 1152921504606846976), which is equal to '0xFFFFFFFFFFFFFFFE' or in other words: "The overflow will result in a value that is 'UI64_MAX - value_you_add''', which is

0xFFFFFFFFFFFFFFFE - 0x7FFFFFFFFFFFFFFE == 0x1000000000000000

Just as I wrote above ;o)
0
 

Author Comment

by:perlperl
ID: 39601968
SUM is LOW - 1
Please look at my very first comment

       uint64_t LOW = 9223372036854775807;   // This is 1<<63 - 1;
      uint64_t LEN = 18446744073709551615;
      uint64_t SUM = LOW + LEN;

      printf  ("SUM = %" PRIu64 "\n", SUM);

SUM = 9223372036854775806  (This is LOW - 1)

Sorry for being dumb :( but I just want to make sure I understand fully why the runtime is doing this math
0
 
LVL 86

Expert Comment

by:jkr
ID: 39601982
Nope - try a hex calculator. '-1' would mean the LSB would be removed, in your case it's the MSB of the quadword.
0
 

Author Comment

by:perlperl
ID: 39602519
Now I finally understand the bit level math behind this overflow ;) I am sure you might have explained in one of your comment but i just could not relate to it and I was digging more about bit arithmetic which i did many years back in undergrad ;)


   9223372036854775807            1 << 63  -   1
+ 18446744073709551615          1 << 64  -   1
--------------------------------------------------------------------

      0111 FFFFFFFFFFFFFF F  
 +   1111 FFFFFFFFFFFFFF F
-----------------------------------------------    
      0111 FFFFFFFFFFFFFF E  (this is indeed 7FFFFFFFFFFFFFFE ;) )

Simplifying further

    0111 FFFFFFFFFFFFFF 1111     =     9223372036854775807
+  1111 FFFFFFFFFFFFFF 1111     =     18446744073709551615
---------------------------------------
     0111 FFFFFFFFFFFFFF 1110    =      9223372036854775806

Ideally the sum should be     1  0111 FFFFFFFFFFFFFF 1110  
but there is no way to store the overflowed 1 in the MSB and it gets ignored (since the max bits can be 64 in uint64)

This is a nice reference:
http://chortle.ccsu.edu/AssemblyTutorial/zAppendixE/binaryAdd.html
0
 

Author Comment

by:perlperl
ID: 39603218
I would like to close this Q.

JKR, can you please tell me which of your comment corresponds to my last comment (http://www.experts-exchange.com/Programming/Languages/CPP/Q_28277370.html?anchor=a39602519) so that I can accept that as a solution.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39604373
I'd use my 1st comment, simply because - with the 'correction' as the followup covers your main question.
0
 

Author Closing Comment

by:perlperl
ID: 39604432
As per jkr, I am accepting this comment.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39604496
Thanks, but may I ask why a 'B'?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to ASCII 1 184
Problem to show menu 5 88
Should CArray be used for a list of pointers in C++? 19 103
How do I set up two external hard drives with Acronis True Image? 12 89
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question