We all know that in C# we can't cast bool to int. I wanted to see what is the binary representation of true with bitmask, but I can't use (bool & int).. I think the problem is the architecture desicion "true is true, not any number != 0" (C++) and I was wondering what the benefits of such an architecture are? What is so bad with the C true/false concept?
我们都知道在c#中不能将bool转换为int,我想知道用位掩码表示true的二进制表示是什么,但是我不能使用(bool & int)。我认为问题在于体系结构的要求是“是真的,不是任何数字!= 0”(c++),我想知道这样一个体系结构的好处是什么?C真/假概念有什么不好?
9 个解决方案
#1
26
It's clearer to the programmer when an integer can't be used for true
or false
.
当一个整数不能用于真或假时,程序员会更清楚。
if (5 > 0)
is easier to understand rather than if(5)
如果(5 > 0)更容易理解,而不是(5)
It's the same reason why they don't allow fall through conditions in switch statements. It's too easy to make a mistake.
这也是为什么它们不允许在交换语句的条件下跌落的原因。犯错误太容易了。
#2
21
In C integers are frequently doubly used as a normal number and a boolean, such as for loops that run while a certain number is not zero. That's a use which clearly has its merits for concise code, like a naïve strlen routine:
在C型整数中,常被重复使用为一个普通数字和一个布尔值,例如在一个特定的数字不为零的情况下运行的循环。这一用法显然具有简洁代码的优点,就像一个朴素的strlen例程:
const char *s;
for (s = str; *s; ++s)
;
return (s - str);
but while short it masks the true purpose of that snippet, which essentially says "loop while the character I am looking at is not the null character". But written down it just says "treat it as a boolean when I feel like it, and as a number in some other cases".
虽然很短,但它掩盖了这段代码的真正目的,它本质上说“循环,而我正在查看的字符不是空字符”。但是写下来只是说“当我喜欢它的时候,把它当作布尔值,在其他一些情况下,把它当作一个数字”。
This kind of double nature allegedly frequently leads to problems and it makes the code less readable (since you have to judge from the context whether a given usage of an int
is intended as int
or bool
).
这种所谓的双重性质经常导致问题,它使代码可读性更差(因为您必须从上下文判断给定的int用法是用于int还是bool)。
If you absolutely need to use a boolean as an integer you can either use
如果您绝对需要使用布尔作为整数,那么您可以使用它
Convert.ToInt32(someBool)
as Noldorin mentioned or roll your own with
就像Noldorin提到的或者是你自己的
someBool ? 1 : 0
Both of which clearly say that you are using an int
(and not a bool
).
两者都清楚地表明您正在使用int(而不是bool)。
#3
15
Just for information, Convert.ToInt32(fooBool)
should tell you that true
is represented by 1, and false
by 0. (This is an arbitrary representation in the BCL, and not necessarily the one used it memory, however.)
对于信息,Convert.ToInt32(fooBool)应该告诉你,true表示为1,而false为0。(这是BCL中的一个任意表示,但不一定是使用it内存的那个。)
The point here is, the bit representation really ought to be meaningless to any programmer. Booleans are meant to be used for specific purposes (i.e. flags), and shouldn't be mixed with ints, else the uses of variables can potentially become quite confusing.
这里的重点是,位表示对任何程序员来说都毫无意义。布尔值用于特定的目的(即标记),不应该与ints混合,否则变量的使用可能会变得非常混乱。
#4
5
I don't think the issue is some much about true/false or 1/0 as it is about the decision to make C# a strongly-typed language rather than a weakly-typed language. There are many benefits (and some drawbacks) to a language being strongly typed. One of the benefits is that it reduces bugs due to misuse (or incorrect use) of expressions.
我不认为问题是关于真/假或1/0,因为它是关于使c#成为强类型语言而不是弱类型语言的决定。对于强类型的语言来说,有许多好处(也有一些缺点)。它的好处之一是减少了由于表达式的误用(或错误使用)而导致的错误。
For example,
例如,
int i = 0;
if (i = 1) {
...
}
won't even compile in C#, but it will both compile and execute incorrectly in C.
甚至不会在c#中编译,但是在C中编译和执行都是不正确的。
Choosing to make C# a strongly-typed language infers these benefits to C# programmers. Even so, they could have introduced a conversion from bool to int (and back) if they choose. I suspect that they didn't do so due to the potential for bugs like that above to be introduced.
选择使c#成为强类型的语言,将为c#程序员带来这些好处。即使如此,如果他们选择的话,他们也可以引入从bool到int(和返回)的转换。我怀疑他们没有这么做,因为有可能会出现上面提到的那样的bug。
#5
4
we can use Convert.ToInt32, its definition is here:
我们可以用转换。ToInt32,它的定义如下:
public static int ToInt32(bool value);
#6
3
Nobody likes all of any other entity's decisions... just write your own converter (quick-&-dirty-like)
没有人喜欢任何其他实体的决定……只需编写自己的转换器(类似于快速-&-脏)
#7
-1
You make a good point on these. However it does remove one shortcut.
你说得很有道理。不过,它确实删除了一条捷径。
With c, toggles can be easily done by the code "bBool = 1 - bBool".
使用c,切换可以通过代码“bBool = 1 - bBool”轻松完成。
If bBool is true (1), then 1 - 1 = 0. bBool changes to false.
如果bBool为真(1),则1 - 1 = 0。bBool更改为false。
If bBool is false (0), then 1 - 0 = 1. bBool changes to true.
如果bBool为false(0),则1 - 0 = 1。bBool更改正确。
Instead in C# I'm required to do a conditional to check the state, and then set it to it's opposite, which causes more work for the machine and me.
相反,在c#中,我被要求做一个条件来检查状态,然后将它设置为相反的状态,这会给机器和我带来更多的工作。
#8
-2
number != 0, is too prone to error.
数字!= 0,太容易出错。
C# made a lot of design decisions to subvert common mistakes like this can lead to.
c#做了很多设计决定来颠覆常见的错误,像这样可以导致。
#9
-2
int n = (bBool) ? 1 : 0
int n = (bBool) ?1:0
#1
26
It's clearer to the programmer when an integer can't be used for true
or false
.
当一个整数不能用于真或假时,程序员会更清楚。
if (5 > 0)
is easier to understand rather than if(5)
如果(5 > 0)更容易理解,而不是(5)
It's the same reason why they don't allow fall through conditions in switch statements. It's too easy to make a mistake.
这也是为什么它们不允许在交换语句的条件下跌落的原因。犯错误太容易了。
#2
21
In C integers are frequently doubly used as a normal number and a boolean, such as for loops that run while a certain number is not zero. That's a use which clearly has its merits for concise code, like a naïve strlen routine:
在C型整数中,常被重复使用为一个普通数字和一个布尔值,例如在一个特定的数字不为零的情况下运行的循环。这一用法显然具有简洁代码的优点,就像一个朴素的strlen例程:
const char *s;
for (s = str; *s; ++s)
;
return (s - str);
but while short it masks the true purpose of that snippet, which essentially says "loop while the character I am looking at is not the null character". But written down it just says "treat it as a boolean when I feel like it, and as a number in some other cases".
虽然很短,但它掩盖了这段代码的真正目的,它本质上说“循环,而我正在查看的字符不是空字符”。但是写下来只是说“当我喜欢它的时候,把它当作布尔值,在其他一些情况下,把它当作一个数字”。
This kind of double nature allegedly frequently leads to problems and it makes the code less readable (since you have to judge from the context whether a given usage of an int
is intended as int
or bool
).
这种所谓的双重性质经常导致问题,它使代码可读性更差(因为您必须从上下文判断给定的int用法是用于int还是bool)。
If you absolutely need to use a boolean as an integer you can either use
如果您绝对需要使用布尔作为整数,那么您可以使用它
Convert.ToInt32(someBool)
as Noldorin mentioned or roll your own with
就像Noldorin提到的或者是你自己的
someBool ? 1 : 0
Both of which clearly say that you are using an int
(and not a bool
).
两者都清楚地表明您正在使用int(而不是bool)。
#3
15
Just for information, Convert.ToInt32(fooBool)
should tell you that true
is represented by 1, and false
by 0. (This is an arbitrary representation in the BCL, and not necessarily the one used it memory, however.)
对于信息,Convert.ToInt32(fooBool)应该告诉你,true表示为1,而false为0。(这是BCL中的一个任意表示,但不一定是使用it内存的那个。)
The point here is, the bit representation really ought to be meaningless to any programmer. Booleans are meant to be used for specific purposes (i.e. flags), and shouldn't be mixed with ints, else the uses of variables can potentially become quite confusing.
这里的重点是,位表示对任何程序员来说都毫无意义。布尔值用于特定的目的(即标记),不应该与ints混合,否则变量的使用可能会变得非常混乱。
#4
5
I don't think the issue is some much about true/false or 1/0 as it is about the decision to make C# a strongly-typed language rather than a weakly-typed language. There are many benefits (and some drawbacks) to a language being strongly typed. One of the benefits is that it reduces bugs due to misuse (or incorrect use) of expressions.
我不认为问题是关于真/假或1/0,因为它是关于使c#成为强类型语言而不是弱类型语言的决定。对于强类型的语言来说,有许多好处(也有一些缺点)。它的好处之一是减少了由于表达式的误用(或错误使用)而导致的错误。
For example,
例如,
int i = 0;
if (i = 1) {
...
}
won't even compile in C#, but it will both compile and execute incorrectly in C.
甚至不会在c#中编译,但是在C中编译和执行都是不正确的。
Choosing to make C# a strongly-typed language infers these benefits to C# programmers. Even so, they could have introduced a conversion from bool to int (and back) if they choose. I suspect that they didn't do so due to the potential for bugs like that above to be introduced.
选择使c#成为强类型的语言,将为c#程序员带来这些好处。即使如此,如果他们选择的话,他们也可以引入从bool到int(和返回)的转换。我怀疑他们没有这么做,因为有可能会出现上面提到的那样的bug。
#5
4
we can use Convert.ToInt32, its definition is here:
我们可以用转换。ToInt32,它的定义如下:
public static int ToInt32(bool value);
#6
3
Nobody likes all of any other entity's decisions... just write your own converter (quick-&-dirty-like)
没有人喜欢任何其他实体的决定……只需编写自己的转换器(类似于快速-&-脏)
#7
-1
You make a good point on these. However it does remove one shortcut.
你说得很有道理。不过,它确实删除了一条捷径。
With c, toggles can be easily done by the code "bBool = 1 - bBool".
使用c,切换可以通过代码“bBool = 1 - bBool”轻松完成。
If bBool is true (1), then 1 - 1 = 0. bBool changes to false.
如果bBool为真(1),则1 - 1 = 0。bBool更改为false。
If bBool is false (0), then 1 - 0 = 1. bBool changes to true.
如果bBool为false(0),则1 - 0 = 1。bBool更改正确。
Instead in C# I'm required to do a conditional to check the state, and then set it to it's opposite, which causes more work for the machine and me.
相反,在c#中,我被要求做一个条件来检查状态,然后将它设置为相反的状态,这会给机器和我带来更多的工作。
#8
-2
number != 0, is too prone to error.
数字!= 0,太容易出错。
C# made a lot of design decisions to subvert common mistakes like this can lead to.
c#做了很多设计决定来颠覆常见的错误,像这样可以导致。
#9
-2
int n = (bBool) ? 1 : 0
int n = (bBool) ?1:0