简体   繁体   中英

Warning : left shift count >= width of type

I have declared this enum in my header file:

enum wildcard {
  ....
  ....
  NW_SRC  = 0x111UL << 40,
  ....
};

When I compile it I get the following warning:

warning: left shift count >= width of type [enabled by default]

I think this is because enum type is treated as an int by the compiler. How to I resolve this?

You have two different problems, first the operation and then the declaration of the constant.

For the operation you could use the macro that is provided in inttypes.h

UINT64_C(0x111) << 40

to have a constant of the appropriate width.

But enumeration constants are int by definition of the standard, so this will not help you to define you an enumeration constant that is large enough to hold the value if on your platform int is only 32 bit wide (which is very likely).

The UL on your platform is probably 32 bit. You may need to use ULL instead:

enum wildcard {
    ....
    ....
    NW_SRC  = 0x111ULL << 40,
    ....
};

This will fix the warning, but the result of the expression may not necessarily fit in an enum (see this answer for details and references to the relevant standard documents).

UL is unsigned long, but long on the majority of compilers is 32-bit. You want ULL for unsigned long long.

But as correctly pointed out by Jens Gustedt in their answer, in C an enum cannot hold values larger than an int anyway so this will not help. C++, in contrast, does allow enums to be represented by larger integral types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM