archangelmihail

FourthBitCheck

Nov 17th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 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.  
  8. class FourthBitCheck
  9. {
  10. static void Main(string[] args)
  11. {
  12. //Write a boolean expression for finding if the bit 3 (counting from 0) of a given integer is 1 or 0.
  13. Console.WriteLine("Please enter your number:");
  14. int number = int.Parse(Console.ReadLine());
  15. Console.WriteLine("Please enter the position you want to check:");
  16. int position = int.Parse(Console.ReadLine());
  17. int mask = 1 << position;
  18. int check = number & mask;
  19. int bit = check >> position;
  20. Console.WriteLine("The bit at position {0} is : {1} ",position,bit); // 1
  21. Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment