Confusion on addition sequence for integers

 

Hello,


i have a simple question.

why is this working:

// Access operator
T operator[](const int idx) const
{ return((_ptr == -1) ? _empty : buffer[(idx < NULL) ? (_ptr - (idx + 1)) : idx]); }


and this is not:

// Access operator
T operator[](const int idx) const
{ return((_ptr == -1) ? _empty : buffer[(idx < NULL) ? ((_ptr - idx) + 1) : idx]); }


I was expecting the terms

(_ptr - (idx + 1))

and

((_ptr - idx) + 1)

to give the same result. 

but they dont...


Any explanation I am missing?


For completness of code here is the whole struct:

    ///////////////////////////////////////
    //
    //  First In Last Out Buffer
    //

        template <typename T>
        struct buffer_filo
        {
            private:
            
                // Internal state
                int     _size;
                int     _ptr;
    
                // Buffer
                T       buffer[];
                T       _empty;

                        
            public:

                // Default constructor
                buffer_filo() :
                    _size(NULL),
                    _ptr(-1)
                { };


            // Buffer functions

                // Reset
                void reset() 
                { 
                    ArrayFree(buffer); 
                    _size = NULL; 
                    _ptr = -1; 
                };
                
                // Size
                int size() const
                { return(_ptr + 1); };
            
                // Assignment operator
                void operator=(const buffer_filo& p_in)
                {
                    _size = p_in.size();
                    _ptr = ArrayResize(buffer, _size, (_size < 512) ? 1024 : (_size >> 1)) - 1;
                    for(int cnt = NULL; (cnt < _size) && !_StopFlag; cnt++)
                    { buffer[cnt] = p_in[cnt]; }
                };
                
                // Access operator
                T operator[](const int idx) const
                { return((_ptr == -1) ? _empty : buffer[(idx < NULL) ? (_ptr - (idx + 1)) : idx]); }
                

            // Push functions

                // Push new element
                bool push(const T& p_in)
                {
                    _ptr            = (_ptr == _size - 1) ? ArrayResize(buffer, _size + 1, (_size < 512) ? 1024 : (_size >> 1)) - 1 : (_ptr + 1); 
                    _size          += (_ptr == _size);
                    buffer[_ptr]    = p_in; 
                    return(_ptr > -1);
                };

                // Push by assignment
                bool operator=(const T& p_in)
                { return(push(p_in)); };


            // Pop functions

                // Pop last element
                T pop()
                { return((_ptr < NULL) ? _empty : buffer[_ptr--]); };

                // Pop by decrement
                T operator--(int)
                { return(pop()); };
                
                // Pop by subtraction
                void operator-=(const int p_in)
                { 
                    int cnt = p_in;
                    while( (cnt > NULL)
                        && (_ptr > -1) )
                    { pop(); cnt--; } 
                };
        };
 
Dominik Egert:

Hello,


i have a simple question.

why is this working:


and this is not:


I was expecting the terms

and

to give the same result. 

but they dont...


Any explanation I am missing?


For completness of code here is the whole struct:

The signs, lets substitute idx for -1

first
x - (-1 + 1)
x - 0

the second
    v becomes positive
(x - -1) + 1
(x + 1) + 1
x+2

 
Alexandre Borela #:

The signs, lets substitute idx for -1

first
x - (-1 + 1)
x - 0

the second
    v becomes positive
(x - -1) + 1
(x + 1) + 1
x+2

Ohh, yes, of course... (OMG)

Thank you for showing me.