我想知道为什么True等于-1而不是1.如果我在C中正确记得(在日子里),"true"将等于1.
Dim t, f As Integer
t = True
f = False
Console.WriteLine(t) ' -1
Console.WriteLine(f) ' 0
Console.ReadLine()
Run Code Online (Sandbox Code Playgroud) 所以我有这个简单的函数,它假设返回true或false.True仅返回2%的时间,并且每隔一段时间返回false.但是,每当我尝试编译完整代码时,我都会收到错误消息;
在此范围C++中未声明"TRUE"
我知道我可以使用int
而不是bool
只使用1
true和0
false,但我想弄清楚为什么这不起作用.有帮助吗?
这是我的功能:
bool Bunny::determineMutant()
{
int rand_num = rand() % 100 + 1; //random num 1-100
if(rand_num == 1 || rand_num == 2) return TRUE;
else return FALSE;
}
Run Code Online (Sandbox Code Playgroud) 让我们考虑这段代码:
static void Main(string[] args)
{
Console.WriteLine(Marshal.SizeOf(typeof(bool)));
Console.WriteLine(String.Join(", ", BitConverter.GetBytes(true)));
}
Run Code Online (Sandbox Code Playgroud)
如果bool是1个字节,我希望它输出
1
1
如果bool是4个字节(作为一个int),我期望
4
1,0,0,0 //让我们忘记字节序
但是,它输出(在x64中)
4
1
在封送代码中,这对我来说是个大问题。我应该信任谁?