Increment / Decrement Unsigned Integer?

Discussion in 'C++' started by Immortal Nephi, Dec 5, 2009.

  1. Largest integer number is used for test condition such as if else,
    while loop and for loop. 32 bit integer number is used on 32 bit
    machine or 64 bit integer number is used on 64 bit machine.

    Signed integer number is the best practice. Sometimes, I prefer to
    use 8 bit integer number because I don’t need large integer number.

    For example:

    signed char value = 0;

    void Increment()
    {
    value++;

    if( value > 0xF )
    value--;
    }

    void Decrement()
    {
    value--;

    if( value < 0x0 )
    value++;
    }

    I decide to choose unsigned 8 bit integer number because two
    variables cannot be combined into signed integer number and unsigned
    integer number. Cast conversion is sometimes confused and possibly,
    the code runs into bugs.

    For example:

    unsigned char value = 0;

    void Increment()
    {
    value++;

    if( value == 0x10 )
    value--;
    }

    void Decrement()
    {
    value--;

    if( value == 0xFF )
    value++;
    }

    I avoid to use –1 instead of 0xFF because C++ Compiler does not know
    value to be 32 bit or 64 bit. It will choose 0xFFFFFFFF on 32 bit
    machine or 0xFFFFFFFFFFFFFFFF on 64 bit machine.

    You will be asking performance issue. 8 bit integer number in for
    loop and 32 bit / 64 bit in for loop run to be identical performance
    unless you use the range 0 through 255.

    The performance will degrade if you use *two* 8 bit for loop instead
    of *one* 32 bit for loop because you choose to use range 0 through
    4,294,967,295.
    Please share your opinion about C++ practice.
     
    Immortal Nephi, Dec 5, 2009
    #1
    1. Advertising

  2. Immortal Nephi

    Stefan Ram Guest

    Immortal Nephi <> writes:
    >signed char value = 0;(...)value++;


    »If during the evaluation of an expression, the result
    is (...) not in the range of representable values for
    its type, the behavior is undefined (...)«

    ISO/IEC 14882:2003(E), 5p5

    >Please share your opinion about C++ practice.


    I believe, once undefined behavior has happened,
    it does not matter anymore, what you do thereafter.

    In C, the behavior was defined for /unsigned/ integers,
    IIRC. I do not know whether such a clause exists in C++.
     
    Stefan Ram, Dec 5, 2009
    #2
    1. Advertising

  3. Immortal Nephi

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >In C, the behavior was defined for /unsigned/ integers,
    >IIRC. I do not know whether such a clause exists in C++.


    »Unsigned integers, declared unsigned, shall obey the
    laws of arithmetic modulo 2^n where n is the number of
    bits in the value representation of that particular size
    of integer«

    ISO/IEC 14882:2003(E), 3.9.1p4
     
    Stefan Ram, Dec 5, 2009
    #3
  4. Immortal Nephi

    Jonathan Lee Guest

    On Dec 5, 1:01 pm, Immortal Nephi <> wrote:
    >         I avoid to use –1 instead of 0xFF because C++ Compiler does not know
    > value to be 32 bit or 64 bit.  It will choose 0xFFFFFFFF on 32 bit
    > machine or 0xFFFFFFFFFFFFFFFF on 64 bit machine.


    Why test against -1 (or, 0xFF) *after* the decrement? Why not test
    for zero *before*? The following should be equivalent to your code:

    void Increment() { if (value != 0xF) ++value; }
    void Decrement() { if (value != 0) --value; }

    This way you don't need to know what "-1" will be (32-bit, 64-bit, 8-
    bit,
    whatever-bit).

    >         You will be asking performance issue.


    No, not really. This seems like a micro-optimization. Unless you have
    a very small loop I doubt the overhead of incrementing the counter
    will
    be significant.

    --Jonathan
     
    Jonathan Lee, Dec 5, 2009
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Jonathan Lee wrote:

    > On Dec 5, 1:01 pm, Immortal Nephi <> wrote:
    >> I avoid to use –1 instead of 0xFF because C++ Compiler does not know
    >> value to be 32 bit or 64 bit. It will choose 0xFFFFFFFF on 32 bit
    >> machine or 0xFFFFFFFFFFFFFFFF on 64 bit machine.

    >


    ~0u is always the largest value of unsigned;
    ~0ul is always the largest value of unsigned long;
    ~0ull is always the largest value of unsigned long long.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEARECAAYFAksjtOwACgkQG6NzcAXitM+HZgCgkUpCIjj+RSoY+IkdVINOiUAf
    XjkAn1sTcYoybZhzimPwggRwt1oUaWFf
    =1tmb
    -----END PGP SIGNATURE-----
     
    Michael Tsang, Dec 12, 2009
    #5
  6. Immortal Nephi

    Jonathan Lee Guest

    On Dec 12, 10:21 am, Michael Tsang <> wrote:
    > ~0u is always the largest value of unsigned;
    > ~0ul is always the largest value of unsigned long;
    > ~0ull is always the largest value of unsigned long long.


    True, but I think what he really wants is 255, which just
    happens to be -1 for an unsigned char (where CHAR_BIT == 8).
    He doesn't actually want -1. If I'm reading the OP correctly.

    --Jonathan
     
    Jonathan Lee, Dec 12, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrix
    Replies:
    1
    Views:
    1,705
    Ivan Vecerina
    Jun 29, 2003
  2. Mark Turney
    Replies:
    11
    Views:
    4,319
    dibeas
    Nov 13, 2006
  3. John
    Replies:
    4
    Views:
    1,162
    Vladimir Marko
    May 17, 2005
  4. Ian Pilcher

    Increment, decrement, overflow, and underflow

    Ian Pilcher, Jan 20, 2005, in forum: C Programming
    Replies:
    5
    Views:
    606
    CBFalconer
    Jan 21, 2005
  5. lovecreatesbeauty
    Replies:
    8
    Views:
    1,723
    Old Wolf
    Sep 12, 2005
Loading...

Share This Page