Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Compiler warning for large unsigned int

Tags:

I have following array, that I need to operate by hand on bitmaps.

const unsigned int BITS[32] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 
                               2048, 4096, 8192, 16384, 32768, 65536, 131072, 
                               262144, 524288, 1048576, 2097152, 4194304, 
                               8388608, 16777216, 33554432, 67108864, 134217728, 
                               268435456, 536870912, 1073741824, 2147483648};

Unfortunately, when compiled I get

warning: this decimal constant is unsigned only in ISO C90

How can I remove this?

like image 466
gruszczy Avatar asked Feb 28 '10 20:02

gruszczy


People also ask

Why not to use unsigned int?

Unsigned integers ( size_t , uint32_t , and friends) can be hazardous, as signed-to-unsigned integer conversions can happen without so much as a compiler warning.

What happens when unsigned int goes negative?

You simply cannot assign a negative value to an object of an unsigned type. Any such value will be converted to the unsigned type before it's assigned, and the result will always be >= 0.

Why we use unsigned int in C?

Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). Note: it is almost always the case that you could use a regular integer variable in place of an unsigned integer.

What is the result when you add together a signed and an unsigned int?

The result is of the same unsigned type. If you mix types of different rank, the higher-ranked type "wins", if it can represent all values of lower-ranked type. The calculations are performed within that type.


1 Answers

Integer literals in C are, by default, of type "signed int" (edit: but see comments for caveats). The last number there is too large to be represented as a signed 32-bit integer, and so you need to tell the compiler that it's an unsigned integer by suffixing it with "U", as:

2147483648U

Note that you can also add a suffix of "L" to make it a "long", but on many systems that's still 32-bit and thus irrelevant.

Also, there's a much less error-prone (and easier-to-read) way to write this code, with the bit-shift operator:

const unsigned int BITS[32] = {1U, 1U<<1, 1U<<2, 1U<<3, 1U<<4,
                               /* and so on */
                               1U<<31};

Or, writing in hexadecimal, if you don't like bit shifts for some reason:

const unsigned int BITS[32] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20,
                               /* and so on */
                               0x80000000U};
like image 121
Brooks Moses Avatar answered Oct 14 '22 11:10

Brooks Moses