- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 ulong
类型的行为有点困惑。我理解它是 64-bit integer used for positive numbers (最大值 18,446,744,073,709,551,615)。我刚开始使用它是因为我需要非常大的正数,但有一些我不理解潜在负数的奇怪行为。
ulong big = 1000000; //Perfectly legal
ulong negative = -1; //"Constant value "-1" cannot be converted to a ulong" Makes sense
//Attempt to sneak around the -1 as a constant compile-time error
int negativeInt = -1;
ulong negativeUlongFail = negativeInt; //"Cannot implicitly convert 'int' to 'ulong'.
//An explicit conversion exists (are you missing a cast?)"
//So I add casting
ulong negativeUlong = (ulong)negativeInt; //But this yields 18446744073709551615
//Try and get a negative number indirectly
ulong number = 0;
number = number - 10; //now number is 18446744073709551615 but no underflow error
这是怎么回事?为什么有些下溢错误会被捕获,而另一些则不会甚至不抛出异常?我可以检测到这些吗?
我专注于通过下溢获得负数,但在获得大于最大值和溢出的数字时我也看到过类似的情况。
我不确定它们在技术上是错误还是异常,所以如果我使用了不正确的术语,请原谅我
最佳答案
如果你想抛出下溢(或溢出)异常,试试这个:-
checked
{
ulong number = 0;
number = number - 10;
}
默认情况下,异常会被吞噬(它以未经检查的方式运行)。
查看官方文档
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked
关于C# ulong 溢出/下溢以某种方式允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45270715/
如果我们在 ulong 表达式和 ulong? 表达式之间使用 == 运算符,则运算符重载 bool ulong(ulong left, ulong right) 似乎被使用。 换句话说,运算符认为两
我正在尝试将字典序列化为 JSON,并出现以下异常: new JavaScriptSerializer().Serialize(mydict)` Type 'System.Collections.Ge
我会将 ulong 值的 bin/oct/dec/hex 值作为字符串。所以我必须使用具有所需基数的 convert.tostring(, base) 。为了支持这一点,我将 ulong 值转换为 l
我有一个枚举,我想检查枚举类型是否为 ulong。 到目前为止尝试过: var checkValue = Enum.GetUnderlyingType(param.ParamType); // pa
我正在处理 std::bitset vector 中的数据,我都必须使用自制函数与 unsigned long(通过 std::bitset::to_ulong())和字符串相互转换(确切的算法与这个
我需要在 C# 中进行巨大的功率计算(想想 2 ^ 1,000,000),而 ulong 远远不够。 .NET 中可能没有任何更大的实现,但是是否有一些第 3 方的东西,或者甚至是我可以做的东西? 最
我目前正在尝试用 C# 制作一个应用程序,它需要一个 ulong number 输入和输出数字的口头表示(即 12 到“十二”,45238 到“四万五千二三十八”等),我希望它能够通过 uLong.M
我对 ulong 类型的行为有点困惑。我理解它是 64-bit integer used for positive numbers (最大值 18,446,744,073,709,551,615)。我
我有一段代码是 // Bernstein hash // http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
我正在尝试适应某些代码,这些代码会在某些数据的头部创建一个 char *。然后能够一次递增该指针 unsigned long 以读取数据。 有人告诉我执行此操作的一个好方法是使用 ifstream 将
这个问题在这里已经有了答案: How to compare Lists in Unit Testing (7 个答案) 关闭 5 年前。 考虑函数: public static ulong[] pr
我正在为加泰罗尼亚语数字编写程序。所以这里是公式: 我决定使用公式的中间部分,因为其他部分对我的知识来说太抽象了(也许我在数学课上睡得太多了)。实际上,我的程序适用于 n = 0;、n = 5;、n
我如何将十六进制/乌龙数的重要部分作为字符串? 例如,如果我有 0x00000321321,我想得到“321321”。 如果我尝试这样的事情: ulong x = 0x0000023632763; s
我想从 64 位字符串中删除位(由无符号长整数表示)。我可以通过一系列掩码和移位操作来完成此操作,或者像下面的代码一样迭代每一位。是否有一些巧妙的位操作方法可以使此操作更快? public ulong
我有以下方法: public static T ExecuteScalar( string query, SqlConnection connection, params Sq
这很好用: Public Const test As ULong = 1 << 30 这不能很好地工作: Public Const test As ULong = 1 << 31 它会产生此错误: C
我正在开发一个“简单”的基数转换器,将基数为 10 的 ULong 转换为任何基数的 String。这里我使用 64 个字符。 Usecase 是缩短 ULong,无论如何都存储为 String。 P
我正在开发一个“简单”的基数转换器,将基数为 10 的 ULong 转换为任何基数的 String。这里我使用 64 个字符。 Usecase 是缩短 ULong,无论如何都存储为 String。 P
如何将表示无符号长整型的 Long/ULong 转换为带填充零的无符号十六进制字符串(16 位十六进制字符串)? 我正在寻找 Kotlin 或 Java 中的简单而简洁的解决方案。 最佳答案 val
下面的代码允许用户输入一个整数并将其转换为二进制。输入的数字是(1) 解析成 long 类型和(2)转化为乌龙型。 namespace ConsoleApplication1 { class Prog
我是一名优秀的程序员,十分优秀!