Advertisement
adriyanbulgary

OperatorsExpressionsAndStatements - Task 11

Jun 13th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. using System;
  2. /*
  3.  * Using bitwise operators, write an expression for finding the value of the bit #3 of a given unsigned integer.
  4.  * The bits are counted from right to left, starting from bit #0. The result of the expression should be either 1 or 0
  5.  */
  6. class ExtractBit
  7. {
  8.     static void Main()
  9.     {
  10.         byte n = 8;  // In binary  = 1000 - The third bit is 1
  11.         Console.Write("Please enter the number: ");
  12.         int number = int.Parse(Console.ReadLine());
  13.         Console.Clear();
  14.  
  15.         Console.WriteLine("The entered number {0} in binary = {1}",number,Convert.ToString(number,2));
  16.         bool thirdBit = (number & n) != 0;
  17.         Console.WriteLine("Is the third bit 1: {0}",thirdBit);
  18.  
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement