// thanks chatgpt for the code that i can use to edit on my own accord
// creator note to self - refer to the |& chatgpt page
using System;
[Flags] // not much functionality, just hints to developers this is going to be used to compare bits
enum MyFlags
{
Flag1 = 1,
Flag2 = 2,
Flag3 = 4,
Flag4 = 8
}
class Program
static void Main()
Console.WriteLine(3 & 3);
MyFlags flags = MyFlags.Flag4 | MyFlags.Flag3; // if you do Console.WriteLine(flags), output: Flags1, Flags3
// flags holds Flag1 and Flag3 and is referenced to it.
// (flags & MyFlags.Flag1) its just looking to see if any of the values in flags contain atleast 1 bit comparable to MyFlags.Flagx
// it is basically just checking to see if flags contains the variable MyFlags.Flagx
// refer to the end of this code to understand comparison bitwise
if ((flags & MyFlags.Flag1) != 0 | (flags & MyFlags.Flag2) != 0)
Console.WriteLine("Flag1 or Flag2 is set");
else
Console.WriteLine("Flag1 and Flag2 are not set");
//one may realize there is a major flaw in that if any of the Flagx contained the number 15, which is 1111,
//it will always return true even if the number in flag is something like 2, which is 0010
//this is primarily why we use powers of 2 for flags.
// 1 - 0001
// 2 - 0010
// 4 - 0100
// 8 - 1000
/*
When looking up what | and & do, you may be confused by what they mean bitwise
if lets say
Console.WriteLine(1 & 2);
it will return 0
because they have 0 in common the bit format
( 0 0 0 1 ) - 1
( 0 0 1 0 ) - 2
It tries to compare each bit
comparison rule
( 0 & 1 ) - returns 0/false
( 1 & 0 ) - returns 0/false
( 0 & 0 ) - returns 0/false
( 1 & 1 ) - returns 1/true
now if you were to do something like
Console.WriteLine(3 & 5);
( 0 0 1 1 ) - 3
( 0 1 0 1 ) - 5
it will return
it just shows each bit comparison and what they return
IT DOES NOT RETURN THE AMOUNT OF BITS THAT RETURN 1
what i mean is if you do 3 & 3, it wont return 2 just because ( 0 0 1 1 ) and ( 0 0 1 1 ) have 2 comparisons that return 1
what its doing is:
compare the first bit in the list: ( 0 & 1 )
whatever it returns, 0 or 1, it will be the first bit in the end result, and then it does that for every other bit
so if you do 3 & 3
( 0 0 1 1 )
what does the first bit return : ( 0 & 0 ), returns 0, so the first bit will be 0
what does the second bit return : ( 0 & 0 ), returns 0, so the second bit will be 0
what does the third bit return : ( 1 & 1 ), returns 0, so the third bit will be 1
what does the fourth bit return : ( 1 & 1 ), returns 0, so the fourth bit will be 1
putting all the return bits together in order, it will be ( 0 0 1 1 ), so it will return 3
this means if you do something like 11 & 2
( 1 0 1 1 ) - 11
11 2
1st ( 1 & 0 ) - returns 0
2nd ( 0 & 0 ) - returns 0
3rd ( 1 & 1 ) - returns 1
4th ( 1 & 0 ) - returns 0
so the end result is ( 0 0 1 0 ), which returns 2
when it comes to |, different comparison rules are applied:
( 0 | 0 ) - returns 0/fase
( 1 | 1 ) - returns 1/true
( 1 | 0 ) - returns 1/true
( 0 | 1 ) - returns 1/true
so if you were to do
5 & 3
it will do the same as &, where it goes down the list of bits, compares them using the | rule, and then adds the end result to the final result
5 3
( 0 | 0 ) - returns 0/false
so the end result will be ( 0 1 1 1 ), which is 7
*/