Increment / Decrement Unsigned Integer?

I

Immortal Nephi

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

Stefan Ram

Immortal Nephi said:
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++.
 
S

Stefan Ram

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
 
J

Jonathan Lee

        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
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


~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-----
 
J

Jonathan Lee

~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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top