C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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."
Version 1: This version of the code uses a ternary expression to convert a bool into an int (1 or 0).
Ternary OperatorVersion 2: Here we use the Convert.ToIn32 method to convert the bool to an int—no ternary is required.
Info: Opening up Convert.ToInt32 up in IL Disassembler, I found it performs the same logic as the ternary (it branches).
C# program that uses bools
using System;
class Program
{
static void Main()
{
// Version 1: convert bool to int.
bool testBool = true;
int testInt = testBool ? 1 : 0;
Console.WriteLine("TEST BOOL: " + testBool); // True.
Console.WriteLine("TEST INT: " + testInt); // 1
// Version 2: convert bool to int.
bool testBool2 = false;
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
Version 1: Here we use the ternary expression to convert the bool to an int (either 0 or 1).
Version 2: This code uses the Convert.ToIn32 method to convert the bool to an int. Its results are identical.
Result: The performance of the two version is the same—use whichever version seems clearest.
C# program that times Convert.ToInt32
using System;
using System.Diagnostics;
class Program
{
const int _max = 100000000;
static void Main()
{
var s1 = Stopwatch.StartNew();
// Version 1: use ternary.
for (int i = 0; i < _max; i++)
{
bool testBool = true;
int testInt = testBool ? 1 : 0;
if (testInt != 1)
{
return;
}
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use Convert.ToInt32.
for (int i = 0; i < _max; i++)
{
bool testBool = true;
int testInt = Convert.ToInt32(testBool);
if (testInt != 1)
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
0.27 ns Use ternary
0.27 ns Use Convert.ToInt32
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
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