HomeSearch

C# Convert Bool to Int

Convert bool values to int values with a ternary expression.
Convert bool, int. A bool can be converted to 0 or 1. In other languages, false is equivalent to 0 and true is equivalent to 1. This is not possible in the C# language.
We convert bools to ints, first running through an example. We then test unsafe code in converting bools to ints. We benchmark to ensure our method is fast.Bool
Example. We cannot implicitly convert from bool to int. The C# compiler uses this rule to enforce program correctness. The same rule mandates you cannot test an int in an if-statement.

Note: When you try to convert a bool into an int with an implicit cast, you receive an error: "Cannot convert type bool to int."

Note 2: I felt there had to be some way to cast the true into a 1, and the false into a 0. But this is not possible.

C# program that uses bools using System; class Program { static void Main() { // Example bool is true. bool testBool = true; // A. // Convert bool to int. int testInt = testBool ? 1 : 0; Console.WriteLine("TEST BOOL: " + testBool); // True. Console.WriteLine("TEST INT: " + testInt); // 1 // Example bool is false. bool testBool2 = false; // B. // Convert bool to int. int testInt2 = Convert.ToInt32(testBool2); Console.WriteLine("TEST BOOL 2: " + testBool2); // False. Console.WriteLine("TEST INT 2: " + testInt2); // 0 } } Output TEST BOOL: True TEST INT: 1 TEST BOOL 2: False TEST INT 2: 0
Internals. Opening up Convert.ToInt32 up in IL Disassembler, I found it tests the bool parameter against true and returns 1 if it is true, or false otherwise.
A benchmark. I benchmarked the two statements and found identical performance. The compiler efficiently inlines Convert.ToInt32(bool) to be the same as the ternary expression in A.

Note: There is more information about the ternary operator on this site. It is useful for small conditional statements.

Ternary Operator

Also: Extension methods could solve this problem partly, but they would make most projects more complex.

Extension
StructLayout. It is possible to use a special struct, with a StructLayout attribute, to convert a bool to an int. The int and the bool are stored in the same position in the struct.

So: When we have a true bool, our value is 1, and when we have a false bool, our value is 0.

Note: Thanks to Paul Pacheco for the tip on using StructLayout. Unfortunately this approach is slower than a ternary.

C# program that uses StructLayout for bool conversion using System; using System.Runtime.InteropServices; [StructLayout(LayoutKind.Explicit)] struct BoolConversion { [FieldOffset(0)] public bool boolValue; [FieldOffset(0)] public byte byteValue; } class Program { static void Main() { // Use special BoolConversion type to convert bool to int. bool testBool = true; int testInt = new BoolConversion { boolValue = testBool }.byteValue; Console.WriteLine("TEST INT: " + testInt); bool testBool2 = false; int testInt2 = new BoolConversion { boolValue = testBool2 }.byteValue; Console.WriteLine("TEST INT 2: " + testInt2); } } Output TEST INT: 1 TEST INT 2: 0
Unsafe. In C# we can write code that manipulates memory. We can convert a bool to an int in this way. This approach is slower than a ternary, and also unsafe—so it is not recommended.
C# program that uses unsafe bool conversion using System; class Program { static unsafe int ConvertBoolUnsafe(bool t) { int i = *(byte*)(&t); return i; } static void Main() { // Use unsafe method to convert booleans to ints. bool testBool = true; int testInt = ConvertBoolUnsafe(testBool); Console.WriteLine("TEST INT: " + testInt); bool testBool2 = false; int testInt2 = ConvertBoolUnsafe(testBool2); Console.WriteLine("TEST INT 2: " + testInt2); } } Output TEST INT: 1 TEST INT 2: 0
A summary. Use a ternary or if-statement to convert from bool to int. I suggest that the ternary statement above is best, as it involves the fewest characters to type and is simple.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls