user2000950
user2000950

Reputation: 561

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

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;

Upvotes: 2

Views: 167

Answers (1)

michaeldel
michaeldel

Reputation: 2385

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

Upvotes: 4

Related Questions