Advertisement
ellapt

3.10.CheckBit

Dec 14th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. using System;
  2.  
  3. class CheckBit
  4. {
  5. static void Main()
  6. {
  7. bool checkInput1, checkInput2;
  8. bool checkBitP;
  9. int v; // the number to check
  10. int p; // byte position;
  11. int bitMaskP=1; // initialize the mask
  12.  
  13. Console.WriteLine("Find if the bit at position p (counting from 0) in a given integer number v has value of 1.");
  14. Console.WriteLine("Please, enter an integer number to be checked: ");
  15. string inputInt = Console.ReadLine();
  16. checkInput1=int.TryParse(inputInt, out v);
  17. Console.WriteLine("Please, enter the number of bit (0-31): ");
  18. inputInt = Console.ReadLine();
  19. checkInput2=(int.TryParse(inputInt, out p));
  20. if(checkInput1&&checkInput2&&p<32)
  21. {
  22. for (int i = 0; i < p; i++)
  23. {
  24. bitMaskP=bitMaskP << 1; //shift the mask p positions to the left;
  25. }
  26. checkBitP=((v & bitMaskP)!=0);
  27. Console.WriteLine("The bit {0} of the integer number {1} (HexDec {1:X}) is EQUAL to 1: {2} ", p,v,checkBitP);
  28. }
  29. else
  30. {
  31. Console.WriteLine("Wrong data!");
  32. }
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement