English 中文(简体)
页: 1 MinValue犯错误
原标题:-1 * int.MinValue gives an error
  • 时间:2011-11-24 11:34:19
  •  标签:
  • c#
  • .net
  • int

当我写这本法典时,我写了该法典。

Console.WriteLine(-1 * int.MinValue);

int a = -1 * -2147483648;

I get an err或

作业流程按检查方式汇编时间。

Is there compile time check especially f或 this value? 或 is it counting all written expressions real time?

最佳回答

汇编员能够在汇编时间而不是操作时间时进行(多重复)计算,因此这样做。

由于-1* int.MinValue (-1 * -2,147,483,648, 因此,2,147,483,648)大于int.MaxValue(2,147,483,647), 它给你带来了编辑错误。

证明是你确切陈述的特例,试图:

Console.WriteLine(-2 * 1073741826);

你们会采取同样的行为。

问题回答

Int(或相反,Int32)可以持有任何32个借方的激化价值。 从2147483648到2147483647不等。

页: 1 这超出了最大Int32值。 这是你外流的原因。

如果你想要进行计算,你就可以将其中一项价值投到Int64。 因此,这项工作应当:

(Int64)(-1)*int.MinValue;

无需进行特别检查——价值自然溢出。 它就像:

Console.WriteLine(int.MaxValue + 1);

这明显超过<代码>int,其价值与int。 MinValue * 1

C第4号第7.19节中:

除非明确表述不固定,否则,在综合算术业务中发生的超支和在汇编时间评价表达时的转换总是造成编辑错误。

对价值观进行了时间检查。 内在价值为:21474048至+2147404747(湿度:2 补充。 因此,你可以例外。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签