Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise operation to a List<bool>

Tags:

c#

list

linq

I have a List<bool> and want to bitwise XOR the list (to create check bits)

here is what I have currently

List<bool> bList = new List<bool>(){true,false,true,true,true,false,false};
bool bResult = bList[0];

for( int i = 1;i< bList.Count;i++)
{
    bResult ^= bList[i];
}

Q: Is there a Linq one-liner to solve this more elegant?

like image 865
Impostor Avatar asked Jun 23 '16 08:06

Impostor


People also ask

Can you use bitwise operators on Booleans?

Mixing bitwise and relational operators in the same full expression can be a sign of a logic error in the expression where a logical operator is usually the intended operator.

What is >> in bitwise?

>> Indicates the bits are to be shifted to the right. Each operand must have an integral or enumeration type. The compiler performs integral promotions on the operands, and then the right operand is converted to type int . The result has the same type as the left operand (after the arithmetic conversions).

Is && A bitwise operator?

A Bitwise And operator is represented as '&' and a logical operator is represented as '&&'. The following are some basic differences between the two operators. a) The logical and operator '&&' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value.

What is the >>> bitwise operator in Java?

A bitwise operator in Java is a symbol/notation that performs a specified operation on standalone bits, taken one at a time. It is used to manipulate individual bits of a binary number and can be used with a variety of integer types – char, int, long, short, byte.


1 Answers

bool bResult = bList.Aggregate((a, b) => a ^ b);
like image 108
Buh Buh Avatar answered Oct 03 '22 19:10

Buh Buh