Reputation: 2813
I read that it is not recommended to rely on multiplying Boolean (True) value or use it in calculations. Also just noticed that in VB.NET True is -1 rather than 1 or any other value.
I have many situations where I'd like to multiply with Boolean True value and treating it as Integer of 1 (or -1 would also make it). What is the suggested way to use Boolean True value in calculations.
Upvotes: 1
Views: 7316
Reputation: 982
Depending on exact circumstances, you might want to use the IComparer or IComparable interfaces. E.g. if you already have some sort of ordering comparison which produces a bool that you then convert and multiply against, it would probably be better just to use one of those interfaces and multiply against the result directly than perform the comparison, get a bool, convert the bool and then multiply.
Watch out, though, since they both can return a negative number depending on the ordering of the arguments. Also, this will probably only be of any use when the bool produced was the product of an ordering comparison to begin with.
Upvotes: 0
Reputation: 42
Initial suggestion:
You can use the Convert.ToInt32(bool)
method to cast the bool to an int.
New suggestion: You can use pointers in C# inside code blocks with the unsafe keyword (NOTE that you also have to enable that in your build options). For more info see: https://www.c-sharpcorner.com/article/pointers-and-unsafe-code/
A code snippet from: https://www.c-sharpcorner.com/article/pointers-in-C-Sharp/
unsafe
{
char c = 'R';
char *pc = &c;
void *pv = pc; // Implicit conversion
int *pi = (int *) pv; // Explicit conversion using casting operator
}
If you make an int pointer to keep the address of the bool, you should be able to make the program read the memory address of the bool as if it was an int. So something along the lines of:
unsafe class Program
{
unsafe static int AddBools(bool* a, bool* b)
{
/*casts the bool pointer to a byte pointer and then uses the *
operator to access the value at the position as a byte, which you can
use in calculations (both data types are 8-bit)*/
return (*(byte*)a) + (*(byte*)b);
}
unsafe static void Main(string[] args)
{
bool a = false;
bool b = true;
Console.WriteLine("a+a = " + AddBools(&a, &a)); //prints 0
Console.WriteLine("a+b = " + AddBools(&a, &b)); //prints 1
Console.WriteLine("b+b = " + AddBools(&b, &b)); //prints 2
Console.ReadKey();
}
}
Just keep in mind the difference in bit numbering between signed/unsigned and integer/floating types. Here are links to the wiki page for bit numbering and a list of the different data types and their sizes in C#: https://en.m.wikipedia.org/wiki/Bit_numbering https://www.tutorialsteacher.com/csharp/csharp-data-types
Upvotes: 0
Reputation: 79461
If you want to use a bool
as the value 0
or 1
, corresponding to false
and true
respectively, then do this:
bool b = true;
double x = 3.14;
double y = (b ? 1 : 0) * x;
Upvotes: 2
Reputation: 273
You could use a ternary operator so that you don't have to guess if true is 1 or -1.
int product = someValue * (myBool ? 1 : 0)
This would also allow you to define other values to true and false.
Upvotes: 1
Reputation: 726569
If you need to make a calculation based on a value of Boolean
, use ternary operator in C#, or the IF
expression in VB.NET:
Res = SomeValue * If(MyBoolean, 1, 0) + SomeOtherValue
Expressions like that make your intentions clear to the reader, which improves readability even in languages that assign Booleans specific numeric values (e.g. C or C++).
Upvotes: 4
Reputation: 172428
Dont know why you want to use a Boolean value for calculation. They are not meant for arithmetic operations.
Dont know if thats the best way but you can do this:-
bool b = true;
var x = 0;
if(b == true)
{
x = -1; // Use the value of this x anywhere you want provided the scope is clear.
}
Upvotes: 0
Reputation: 39248
If you rely on this extensively I would suggest to put an extension method on bool that returns a numeric value of your choosing - based on true/false.
Upvotes: 0