Venciity

BitWiseOperators - set the bit at postion p to 1

Jan 30th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.50 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 ex3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // set the bit at position p to 1
  14.             int p = 2;
  15.             int n = 42;                 //00101010
  16.             int bit = (1 << p);         //00000100
  17.             int result = n | bit;       //00101110
  18.             Console.WriteLine(result);  //46
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment