let myuint64 = 10uL match myuint64 with | -1 -> () | _ -> ()
How do I define the given -1 as a uint64 value?
如何将给定的-1定义为uint64值?
7 个解决方案
#1
if F# will convert it for you then -1UL would work. If not then you can specify it as 0xFFFFFFFFFFFFFFFFUL and add a comment to remember that it's -1.
如果F#会为你转换它,那么-1UL会起作用。如果没有,那么您可以将其指定为0xFFFFFFFFFFFFFFFFUL并添加注释以记住它为-1。
Don't have the F# tools installed at the moment so I cannot verify this.
目前没有安装F#工具,所以我无法验证这一点。
#2
> match 0UL-1UL with
- |System.UInt64.MaxValue -> "-1"
- |_ -> "???"
- ;;
val it : string = "-1"
#3
Let me leave alone the fact that you can't really represent a negative value with a data type that can only store positive values (and zero of course).
让我单独留下这样一个事实:你不能用一个只能存储正值的数据类型来表示负值(当然是零)。
If, on the other hand, you were storing it in a signed value, -1 would be stored as all bits set.
另一方面,如果将它存储在有符号值中,则-1将被存储为所有位设置。
So basically, I will assume you want to find a way to represent -1 as a bit-wise value that will be compatible with -1 as a signed value.
基本上,我假设你想要找到一种方法来表示-1作为一个逐位值,它将与-1作为有符号值兼容。
The value would then be, in C# and C/C++ syntax, 0xffffffffffffffff. Exactly how to specify that in F# I don't know.
那么,在C#和C / C ++语法中,该值将为0xffffffffffffffff。究竟如何在F#中指定我不知道。
#4
I don't know F# at all, but if it's anything like any other languages, a UInt64 can't be -1. Ever. UInt means unsigned integer, which means it can only represent positive values.
我根本不知道F#,但如果它与任何其他语言一样,则UInt64不能为-1。永远。 UInt表示无符号整数,这意味着它只能表示正值。
#5
To expand on other answers: When a type starts with a u it means unsigned. What signed/unsigned means is this:
扩展其他答案:当类型以u开头时,表示未签名。签名/未签名的含义是:
Numbers are stored using a certain number of bits. In the case of int64 and uint64, 64 bits are used. If the number is signed, the 1st bit is not used as part of the number itself, only the other 63 are. That bit is used to say whether the number is negative. If the number is unsigned, then all bits including the 1st bit are used as part of the number and the number is always non-negative (ie: is positive or 0).
使用一定数量的位存储数字。在int64和uint64的情况下,使用64位。如果该号码已签名,则第1位不会用作号码本身的一部分,只有其他63位。该位用于表示该数字是否为负数。如果数字是无符号的,那么包括第1位的所有位都用作数字的一部分,并且数字总是非负的(即:为正或0)。
#6
Well you could assign it -1 and on most architectures store the 2's complement in there. The signed and unsigned stuff are really only for the type checking. There is no negative sign in hardware.
那么你可以指定它-1并且在大多数架构中存储2的补码。签名和未签名的东西实际上只用于类型检查。硬件没有负面信号。
I have no idea if f# type checker is smart enough to know that a lexical constant -1 is a negative number and should not be put in a uint64.
我不知道f#类型检查器是否足够聪明,知道词法常量-1是负数,不应该放在uint64中。
C definitely does not care.
C绝对不在乎。
#include <stdio.h>
#include <inttypes.h>
main()
{
uint64_t x = -1;
printf("0x%x\n", x); // 0xffffffff
}
#7
If you want to go with a signed int:
如果你想使用signed int:
-1: int64
but you can't match a negative number to a uint, as others have stated.
但正如其他人所说,你不能将负数与uint相匹配。
#1
if F# will convert it for you then -1UL would work. If not then you can specify it as 0xFFFFFFFFFFFFFFFFUL and add a comment to remember that it's -1.
如果F#会为你转换它,那么-1UL会起作用。如果没有,那么您可以将其指定为0xFFFFFFFFFFFFFFFFUL并添加注释以记住它为-1。
Don't have the F# tools installed at the moment so I cannot verify this.
目前没有安装F#工具,所以我无法验证这一点。
#2
> match 0UL-1UL with
- |System.UInt64.MaxValue -> "-1"
- |_ -> "???"
- ;;
val it : string = "-1"
#3
Let me leave alone the fact that you can't really represent a negative value with a data type that can only store positive values (and zero of course).
让我单独留下这样一个事实:你不能用一个只能存储正值的数据类型来表示负值(当然是零)。
If, on the other hand, you were storing it in a signed value, -1 would be stored as all bits set.
另一方面,如果将它存储在有符号值中,则-1将被存储为所有位设置。
So basically, I will assume you want to find a way to represent -1 as a bit-wise value that will be compatible with -1 as a signed value.
基本上,我假设你想要找到一种方法来表示-1作为一个逐位值,它将与-1作为有符号值兼容。
The value would then be, in C# and C/C++ syntax, 0xffffffffffffffff. Exactly how to specify that in F# I don't know.
那么,在C#和C / C ++语法中,该值将为0xffffffffffffffff。究竟如何在F#中指定我不知道。
#4
I don't know F# at all, but if it's anything like any other languages, a UInt64 can't be -1. Ever. UInt means unsigned integer, which means it can only represent positive values.
我根本不知道F#,但如果它与任何其他语言一样,则UInt64不能为-1。永远。 UInt表示无符号整数,这意味着它只能表示正值。
#5
To expand on other answers: When a type starts with a u it means unsigned. What signed/unsigned means is this:
扩展其他答案:当类型以u开头时,表示未签名。签名/未签名的含义是:
Numbers are stored using a certain number of bits. In the case of int64 and uint64, 64 bits are used. If the number is signed, the 1st bit is not used as part of the number itself, only the other 63 are. That bit is used to say whether the number is negative. If the number is unsigned, then all bits including the 1st bit are used as part of the number and the number is always non-negative (ie: is positive or 0).
使用一定数量的位存储数字。在int64和uint64的情况下,使用64位。如果该号码已签名,则第1位不会用作号码本身的一部分,只有其他63位。该位用于表示该数字是否为负数。如果数字是无符号的,那么包括第1位的所有位都用作数字的一部分,并且数字总是非负的(即:为正或0)。
#6
Well you could assign it -1 and on most architectures store the 2's complement in there. The signed and unsigned stuff are really only for the type checking. There is no negative sign in hardware.
那么你可以指定它-1并且在大多数架构中存储2的补码。签名和未签名的东西实际上只用于类型检查。硬件没有负面信号。
I have no idea if f# type checker is smart enough to know that a lexical constant -1 is a negative number and should not be put in a uint64.
我不知道f#类型检查器是否足够聪明,知道词法常量-1是负数,不应该放在uint64中。
C definitely does not care.
C绝对不在乎。
#include <stdio.h>
#include <inttypes.h>
main()
{
uint64_t x = -1;
printf("0x%x\n", x); // 0xffffffff
}
#7
If you want to go with a signed int:
如果你想使用signed int:
-1: int64
but you can't match a negative number to a uint, as others have stated.
但正如其他人所说,你不能将负数与uint相匹配。