bitwise operators c# code example

Example 1: C# bitwise operation

ulong x = 73473458374534587UL;
//0 0 0 0 0 0 0 1
//0 0 0 0 0 1 0 1
//0 0 0 0 0 1 1 1
//1 0 1 1 1 0 0 0
//1 0 0 1 1 0 1 0
//1 1 0 1 0 1 0 1
//0 1 1 0 0 1 0 1
//1 0 1 1 1 0 1 1

ulong y = 635528292UL;
//0 0 0 0 0 0 0 0
//0 0 0 0 0 0 0 0
//0 0 0 0 0 0 0 0
//0 0 0 0 0 0 0 0
//0 0 1 0 0 1 0 1
//1 1 1 0 0 0 0 1
//0 1 1 0 0 1 0 0
//0 1 1 0 0 1 0 0

//x|y will get all ths bits set in x or y or both
//x^y will get all the bits set in x or y, but not both
//x&y will get all the bits set in x and y

var bitor = x|y; //73473458997388799
//0 0 0 0 0 0 0 1
//0 0 0 0 0 1 0 1
//0 0 0 0 0 1 1 1
//1 0 1 1 1 0 0 0
//1 0 1 1 1 1 1 1
//1 1 1 1 0 1 0 1
//0 1 1 0 0 1 0 1
//1 1 1 1 1 1 1 1

var exor = x^y; //73473458984714719
//0 0 0 0 0 0 0 1
//0 0 0 0 0 1 0 1
//0 0 0 0 0 1 1 1
//1 0 1 1 1 0 0 0
//1 0 1 1 1 1 1 1
//0 0 1 1 0 1 0 0
//0 0 0 0 0 0 0 1
//1 1 0 1 1 1 1 1

var bitand = x&y; //12674080
//0 0 0 0 0 0 0 0
//0 0 0 0 0 0 0 0
//0 0 0 0 0 0 0 0
//0 0 0 0 0 0 0 0
//0 0 0 0 0 0 0 0
//1 1 0 0 0 0 0 1
//0 1 1 0 0 1 0 0
//0 0 1 0 0 0 0 0

Example 2: bitwise and c#

& //bitwise AND for c#