Advertisement
vencinachev

BitwiseMask

Feb 9th, 2021
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BitwiseOperations
  8. {
  9.     class Program
  10.     {
  11.         static bool Contains(int mask, int el)
  12.         {
  13.             return (mask & (1 << el)) != 0;
  14.         }
  15.  
  16.         static void Insert(ref int mask, int el)
  17.         {
  18.             mask |= (1 << el);
  19.         }
  20.  
  21.         static void Remove(ref int mask, int el)
  22.         {
  23.             mask &= ~(1 << el);
  24.         }
  25.  
  26.         static void Change(ref int mask, int el)
  27.         {
  28.             mask ^= (1 << el);
  29.         }
  30.  
  31.         static void Main(string[] args)
  32.         {
  33.             int a = 0;
  34.             Insert(ref a, 0);
  35.             Insert(ref a, 3);
  36.             Insert(ref a, 5);
  37.             Console.WriteLine(Convert.ToString(a, 2).PadLeft(32, '0'));
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement