Question: This is probably simple but I'm not getting it right. I have a "bitmask" enum which has a value `all` that indicates that all bits are set. However, I can't get it to flip all bits using ~0. The following error appears:

```
<source>:11:16: error: enumerator value '-1' is outside the range of underlying type 'uint_fast8_t' {aka 'unsigned char'}
   11 |         all = ~0x0,
      |             

```

Which is strange because it should actually fit into uint8\_t no? Here is my code ([godbolt](https://godbolt.org/z/MYdnT7hbh)):

```
#include <iostream>

int main()
{
    enum mask_t : uint_fast8_t {
        first = 0x1,
        second = 0x2,
        all = ~0x0,
    } mask;

    mask = all;
}

```

Answer: 
You can get the maximum value with the help of some meta-programming tricks

```
#include <limits>
#include <type_traits>
#include <cstdint>

enum mask_t : uint_fast8_t {
  first = 0x1,
  second = 0x2,
  all = std::numeric_limits<std::underlying_type_t<mask_t>>::max()
} mask;

```

[Demo](https://godbolt.org/z/EcTqrj568)
You need to convert the expression `~0x0` having the type `int` and a negative value to the type `uint_fast8_t` like for example

```
all = static_cast<uint_fast8_t>( ~0x0 ),

```

Why does the expression have the type `int`? Because the integer literal `0x0` has the type `int` and the operator ~ does not change the type of the expression.

From the C++ Standard

> 
> 2 The type of an integer literal is the first of the corresponding
> list in Table 5 in which its value can be represented.
> 
> 
> 

And an object of the type `int` can represent the literal `0x0`.

Here is a demonstration program.

```
#include <iostream>
#include <iomanip>
#include <type_traits>

int main()
{

    std::cout << std::boolalpha << std::is_same_v<int, decltype( 0x0 )> << '\n';
}

```

The program output is

```
true

```

Another way is just to write

```
all = 0xff,

```