//C# Logische Bitsgewijze Operatoren Operatoren public class Example1 { void Main() { Console.WriteLine((7 & 10)); Console.WriteLine((7 | 10)); Console.WriteLine((7 & 10)); Console.WriteLine((7 & (7 == false))); Console.ReadLine(); } } public class Example2 { void Main() { int tamagotchiState = 5; if (((tamagotchiState & 1) == 1)) { Console.WriteLine("happy"); } if (((tamagotchiState & 2) == 2)) { Console.WriteLine("thirsty"); } if (((tamagotchiState & 4) == 4)) { Console.WriteLine("hungry"); } if (((tamagotchiState & 8) == 8)) { Console.WriteLine("tired"); } Console.WriteLine(); Console.WriteLine("making tamagotchi not hungry ..."); tamagotchiState = (tamagotchiState & (4 == false)); Console.WriteLine(); if (((tamagotchiState & 1) == 1)) { Console.WriteLine("happy"); } if (((tamagotchiState & 2) == 2)) { Console.WriteLine("thirsty"); } if (((tamagotchiState & 4) == 4)) { Console.WriteLine("hungry"); } if (((tamagotchiState & 8) == 8)) { Console.WriteLine("tired"); } Console.WriteLine(); Console.WriteLine("making tamagotchi thirsty ..."); tamagotchiState = (tamagotchiState | 2); Console.WriteLine(); if (((tamagotchiState & 1) == 1)) { Console.WriteLine("happy"); } if (((tamagotchiState & 2) == 2)) { Console.WriteLine("thirsty"); } if (((tamagotchiState & 4) == 4)) { Console.WriteLine("hungry"); } if (((tamagotchiState & 8) == 8)) { Console.WriteLine("tired"); } Console.WriteLine(); Console.WriteLine("making tamagotchi happy, hungry and tired ..."); tamagotchiState = (((tamagotchiState | 1) | 4) | 8); Console.WriteLine(); if (((tamagotchiState & 1) == 1)) { Console.WriteLine("happy"); } if (((tamagotchiState & 2) == 2)) { Console.WriteLine("thirsty"); } if (((tamagotchiState & 4) == 4)) { Console.WriteLine("hungry"); } if (((tamagotchiState & 8) == 8)) { Console.WriteLine("tired"); } Console.WriteLine(); Console.WriteLine("making tamagotchi not hungry and not tired ..."); tamagotchiState = ((tamagotchiState & (4 == false)) & (8 == false)); Console.WriteLine(); if (((tamagotchiState & 1) == 1)) { Console.WriteLine("happy"); } if (((tamagotchiState & 2) == 2)) { Console.WriteLine("thirsty"); } if (((tamagotchiState & 4) == 4)) { Console.WriteLine("hungry"); } if (((tamagotchiState & 8) == 8)) { Console.WriteLine("tired"); } Console.WriteLine(); Console.WriteLine("inverting states ..."); tamagotchiState = (tamagotchiState == false); Console.WriteLine(); if (((tamagotchiState & 1) == 1)) { Console.WriteLine("happy"); } if (((tamagotchiState & 2) == 2)) { Console.WriteLine("thirsty"); } if (((tamagotchiState & 4) == 4)) { Console.WriteLine("hungry"); } if (((tamagotchiState & 8) == 8)) { Console.WriteLine("tired"); } Console.ReadLine(); } } //Bezoek www.vbvoorbeelden.be voor meer C# voorbeelden. //Copyright - De Wolf / vbvoorbeelden - 2003-2010 - Alle rechten voorbehouden.