Why can I not assign a binary literal of 32 bits for a int32?

Viewed 70

Why does the compiler fail with this error: 'Cannot implicitly convert uint to int' when I do this?:

int bits = 0b1001_1000_0000_0000_0000_0000_0000_0000;
1 Answers

0b1001_1000_0000_0000_0000_0000_0000_0000 is an uint value. What you can do is explictly convert it to int, here with unchecked to allow for negative numbers (when given value exceeds Int32.MaxValue):

int bits = unchecked((int)0b1001_1000_0000_0000_0000_0000_0000_0000);

In this example you will have bits == -1744830464

Related