The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

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

Low = 9223372036854775807

LEN = 18446744073709551615

SUM = 9223372036854775806

SIZE of off_t = 8

maxeof = 9223372036854775807

maxeof_2 = 0

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);
```

Low = 9223372036854775807

LEN = 18446744073709551615

SUM = 9223372036854775806

SIZE of off_t = 8

maxeof = 9223372036854775807

maxeof_2 = 0

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 ??

how does Ui63_MAX - value_you_add (UI63_MAX = 9223372036854775807. I am not adding 1)

maps to 9223372036854775806

uint64_t SUM = LOW + LEN;

= 9223372036854775807 + 18446744073709551615;

= 9223372036854775806

which is

SUM = 0x7FFFFFFFFFFFFFFF + 0xFFFFFFFFFFFFFFFF

= 0x7FFFFFFFFFFFFFFF - 1 (How in the world compiler does this ? ;) )

= 0x6FFFFFFFFFFFFFFF

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

0xFFFFFFFFFFFFFFFE - 0x7FFFFFFFFFFFFFFE == 0x1000000000000000

Just as I wrote above ;o)

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

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

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.

All Courses

From novice to tech pro — start learning today.

overflowwill 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.