4

I need to assign constant value in integer (or other data type).

I got "Cannot convert ..." error with the assignment.

enter image description here

Casting doesn't seem to work with "Overflow ..." error.

enter image description here

What's wrong with this?

asked Feb 18, 2012 at 17:14
1
  • Hi, there! 0xffffffff is over the limit, use ulong and redefine the import function parameters if this is about PInvoke. Commented Feb 18, 2012 at 17:26

2 Answers 2

4

You've defined too many Fs in the constant. Using 0xFFFFFFFF the compiler must choose a a storage location that supports a positive value of 0xFFFFFFFF. The max positive value of an Int32 is instead 0x7FFFFFFF and hence the compiler correctly errors. The only types which can hold 0xFFFFFFFF are uint or one of the 64 bit storages.

To fix this just use Int32.MaxValue

int i32 = Int32.MaxValue;
answered Feb 18, 2012 at 17:16
Sign up to request clarification or add additional context in comments.

Comments

1

The maximum (largest positive) number a 32 bit int can represent is 0x7fffffff. The 7 at the front occurs rather than another f is because int is signed, so the top bit represents the sign (positive or negative) of the number.

In your first example the compiler sees a number that can only be represented by an unsigned int, so it assumes it is a UInt32. This cannot be converted to an int, hence the error.

In your second example you are forcing the conversion with a cast, but the number won't fit into an int, so an overflow (number too big) occurs.

To avoid an overflow, options include:

  • Assign a -1
  • Assign 0xffffffff in an unchecked {} block so the raw binary value is written without checking for an overflow. This will read back as -1.
  • Use a wider type that can represent the larger positive number (uint or long, for example). This won't be considered a -1 though, it'll be a large positive value.
answered Feb 18, 2012 at 17:24

2 Comments

What do you mean under "can only be represented by an unsigned int"? Isn't 0xFFFFFFFF -1 in two's complement which int supports for sure?
@IC, 0xFFFFFFFF is read by the compiler as the positive decimal value 4294967295, not as -1. As the largest/most positive value that can be represented in a 32-bit signed integer is 2147483647 (0x7fffffff), an overflow exception is thrown to protect you. To forcibly assign this as a raw binary value you could use unchecked { int i = (int) 0xffffffff; } which would skip the overflow check - of course if you read this value back it will be interpreted as (int) -1.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.